手机网站建设服务,上海网站建设过程,萨wordpress,企业头像logo设计免费Rust 中的字符是 Unicode 类型#xff0c;因此每个字符占据 4 个字节内存空间#xff0c;但字符串不一样#xff0c;字符串是 UTF-8 编码#xff0c;也就是字符串中的字符所占的字节数是变化的(1 - 4)。 常见的字符串有两种:
str#xff0c;通常是引用类型#xff0c;因此每个字符占据 4 个字节内存空间但字符串不一样字符串是 UTF-8 编码也就是字符串中的字符所占的字节数是变化的(1 - 4)。 常见的字符串有两种:
str通常是引用类型str即字符串字面常量字符串切片。std::string::String 类型str的变量是被硬编码的快速而高效但不可变类型String是可设可变的它是在堆上的变量如何管理内存有的语言用垃圾回收机制Garbage Collection标记使用情况并自动清理而Rust不愿意用GC既要高性能又要高安全性提出变量离开作用域即自动释放其占用的内存比GC思路更妙。 C语言中清理内存的函数free要被手动调用Rust中则是drop()Rust自动调用。 C中的Resoure Acquisition Is InitializationRAII模型。
两种字符串类型互转
str转String:
let a String::from(hello, world);
hello, world.to_string();String转str,引用即可
fn main() {let s String::from(hello,world!);say_hello(s);say_hello(s[..]);say_hello(s.as_str());
}fn say_hello(s: str) {println!({},s);
}除上述两种类型Rust标准库还有其他类型的字符串。
字符串不能被直接索引
Rust字符串不允许索引操作。由于不同字符占用字节数不等考虑操作时间复杂度不能实现O(1)。
//三个字节
let a 中国人;
//一个字节
let b Chinese;Rust字符串虽然不能索引但可以切片slice类似Python等语言中的切片概念。
fn main() {let my_name kirk zhang;let first_name my_name[0..4];let last_name my_name[5..10];println!({},first_name);println!({},last_name);greet(String::from(my_name));// 尝试my_name[0]报错不过可以用.chars().nth或.bytes()来实现println!(can str be indexed {:?},my_name.chars().nth(0));
}fn greet(name: String){println!(hello {}, name);
}小心字符串切片
注意字符串切片是按字节来的哦而Rust字符串是UTF-8协议格式一般1个字符1个字节但1个中文字符占3个字节如果切片起止不在字符边界则有异常。
fn main(){let f 中国人;let f1 f[0..5];println!(watch out, what you got {},f1);
}提示thread ‘main’ panicked at ‘byte index 5 is not a char boundary; it is inside ‘国’ (bytes 3…6) of 中国人’, src/main.rs:10:15 note: run with RUST_BACKTRACE1 environment variable to display a backtrace
字符串的操作
替换
replace(要被替换的子串新字符串)返回结果字符串。 let word String::from(rust);let new_word word.replace(r,R);println!({}, new_word);插入
insert(位置索引,字符)、insert_str(位置索引,字符串)变量本身变化声明时要mut。 let word String::from(rust);let mut new_word word.replace(r,R);new_word.insert_str(0, I love );println!({}, new_word);追加
push(字符) push_str(字符串) let word String::from(rust);let mut new_word word.replace(r,R);new_word.insert_str(0, I love );new_word.push(!);println!({}, new_word); 连接
或 调用了String的add(selfstr)方法其定义要求后面的参数为字符串字面常量返回String类型。 或用format! 适用于 String 和 str 。format! 的用法与 println! 的用法类似。
let a1 String::from(tic);
let a2 String::from(tac);
let a3 String::from(toe);// 经过下面一行后a1被释放了不能再使用a2、a3依然存在。
let s s1 - s2 - s3;删除
有4个方法pop()、remove(索引位置)、truncate(起始位置)、clear() 注意删除字符串的索引必须是字符串中字符的边界否则错误。
fn main() {let mut string_remove String::from(测试remove方法);println!(string_remove 占 {} 个字节,std::mem::size_of_val(string_remove.as_str()));// 删除第一个汉字string_remove.remove(0);// 下面代码会发生错误// string_remove.remove(1);// 直接删除第二个汉字// string_remove.remove(3);dbg!(string_remove);
}字符串的字符与字节
例 for c in 中国人.chars() {println!({}, c);}for b in 中国人.bytes() {println!({}, b);}