- Learning Rust
- Paul Johnson Vesa Kaihlavirta
- 189字
- 2021-07-02 23:07:16
Back to Strings
As we saw before perting onto the heap and stack, we can also define a string like this:
let mut myString = String::new();
The String:: tells the compiler that we are going to use the standard library, String, and we tell the program that we are going to create a mutable String and store a reference to it on the stack in something called myString.
The dynamic string can be created as either being empty, or with memory preallocated to it. For example, say we want to store the words You'll never walk alone (a total of 23 bytes), preallocating the space for them. This is how to do it:
let mut ynwa = String::with_capacity(23); ynwa.push_str("You'll never walk alone");
This is just a performance optimization and is not typically required, since Strings grow automatically when they need to. The following does roughly the same job:
let mut ynwa = "You'll never walk alone".to_owned();
Rust strings are not null-terminated, and consist entirely of valid Unicode. Therefore, they can contain null bytes and characters from any language, but they may require more bytes than they contain characters.
- Python入門很簡單
- JMeter 性能測試實戰(zhàn)(第2版)
- Java Web開發(fā)之道
- Vue.js 3.x從入門到精通(視頻教學版)
- 數(shù)據(jù)結構(Python語言描述)(第2版)
- INSTANT CakePHP Starter
- Java項目實戰(zhàn)精編
- Modular Programming in Java 9
- 可解釋機器學習:模型、方法與實踐
- Serverless Web Applications with React and Firebase
- Programming Microsoft Dynamics? NAV 2015
- Unity 2017 Game AI Programming(Third Edition)
- Python+Office:輕松實現(xiàn)Python辦公自動化
- WildFly Cookbook
- ASP.NET Core and Angular 2