let hello1 = "Hello, world!";
let hello2 = "Hello, world!".to_string();
let hello3 = String::from("Hello, world!");
Run Code Online (Sandbox Code Playgroud)
She*_*ter 12
let hello1 = "Hello, world!";
Run Code Online (Sandbox Code Playgroud)
这会创建一个字符串slice(&str).具体来说,a &'static str,一个在整个程序期间存在的字符串切片.没有分配堆内存; 字符串的数据存在于程序本身的二进制文件中.
let hello2 = "Hello, world!".to_string();
Run Code Online (Sandbox Code Playgroud)
这使用格式化机制来格式化任何实现的类型Display,创建一个拥有的,已分配的字符串(String).在1.9.0之前的Rust版本中(特别是因为这个提交),这比直接使用转换要慢String::from.在1.9.0及更高版本中,调用.to_string()字符串文字与速度相同String::from.
let hello3 = String::from("Hello, world!");
Run Code Online (Sandbox Code Playgroud)
这会String以有效的方式将字符串切片转换为拥有的,已分配的字符串().
let hello4 = "hello, world!".to_owned();
Run Code Online (Sandbox Code Playgroud)
同样的String::from.
也可以看看:
| 归档时间: |
|
| 查看次数: |
340 次 |
| 最近记录: |