字符串
字符串字面量的类型是 &str, 例如 let s: &str = "hello, world" 中的 "hello, world" 的类型就是 &str。
str 和 &str
- 🌟 正常情况下我们无法使用
str类型,但是可以使用&str来替代
2. // 修复错误,不要新增代码行
3. fn main() {
4. let s: str = "hello, world";
5. }
- 🌟🌟 如果要使用
str类型,只能配合Box。&可以用来将Box<str>转换为&str类型
2. // 使用至少两种方法来修复错误
3. fn main() {
4. let s: Box<str> = "hello, world".into();
5. greetings(s)
6. }
8. fn greetings(s: &str) {
9. println!("{}",s)
10. }
String
String 是定义在标准库中的类型,分配在堆上,可以动态的增长。它的底层存储是动态字节数组的方式( Vec<u8> ),但是与字节数组不同,String 是 UTF-8 编码。
- 🌟
2. // 填空
3. fn main() {
4. let mut s = __;
5. s.push_str("hello, world");
6. s.push('!');
8. assert_eq!(s, "hello, world!");
9. }
- 🌟🌟🌟
2. // 修复所有错误,并且不要新增代码行
3. fn main() {
4. let s = String::from("hello");
5. s.push(',');
6. s.push(" world");
7. s += "!".to_string();
9. println!("{}", s)
10. }
- 🌟🌟 我们可以用
replace方法来替换指定的子字符串
2. // 填空
3. fn main() {
4. let s = String::from("I like dogs");
5. // 以下方法会重新分配一块内存空间,然后将修改后的字符串存在这里
6. let s1 = s.__("dogs", "cats");
8. assert_eq!(s1, "I like cats")
9. }
在标准库的 String 模块中,有更多的实用方法,感兴趣的同学可以看看。
- 🌟🌟 你只能将
String跟&str类型进行拼接,并且String的所有权在此过程中会被 move
2. // 修复所有错误,不要删除任何一行代码
3. fn main() {
4. let s1 = String::from("hello,");
5. let s2 = String::from("world!");
6. let s3 = s1 + s2;
7. assert_eq!(s3,"hello,world!");
8. println!("{}",s1);
9. }
&str 和 String
与 str 的很少使用相比,&str 和 String 类型却非常常用,因此也非常重要。
- 🌟🌟 我们可以使用两种方法将
&str转换成String类型
2. // 使用至少两种方法来修复错误
3. fn main() {
4. let s = "hello, world";
5. greetings(s)
6. }
8. fn greetings(s: String) {
9. println!("{}",s)
10. }
- 🌟🌟 我们可以使用
String::from或to_string将&str转换成String类型
2. // 使用两种方法来解决错误,不要新增代码行
3. fn main() {
4. let s = "hello, world".to_string();
5. let s1: &str = s;
6. }
字符串转义
- 🌟
1. fn main() {
2. // 你可以使用转义的方式来输出想要的字符,这里我们使用十六进制的值,例如 \x73 会被转义成小写字母 's'
3. // 填空以输出 "I'm writing Rust"
4. let byte_escape = "I'm writing Ru\x73__!";
5. println!("What are you doing\x3F (\\x3F means ?) {}", byte_escape);
7. // 也可以使用 Unicode 形式的转义字符
8. let unicode_codepoint = "\u{211D}";
9. let character_name = "\"DOUBLE-STRUCK CAPITAL R\"";
11. println!("Unicode character {} (U+211D) is called {}",
12. unicode_codepoint, character_name );
14. // 还能使用 \ 来连接多行字符串
15. let long_string = "String literals
16. can span multiple lines.
17. The linebreak and indentation here \
18. can be escaped too!";
19. println!("{}", long_string);
20. }
- 🌟🌟🌟 有时候需要转义的字符很多,我们会希望使用更方便的方式来书写字符串: raw string.
1. /* 填空并修复所有错误 */
2. fn main() {
3. let raw_str = r"Escapes don't work here: \x3F \u{211D}";
4. // 修改上面的行让代码工作
5. assert_eq!(raw_str, "Escapes don't work here: ? ℝ");
7. // 如果你希望在字符串中使用双引号,可以使用以下形式
8. let quotes = r#"And then I said: "There is no escape!""#;
9. println!("{}", quotes);
11. // 如果希望在字符串中使用 # 号,可以如下使用:
12. let delimiter = r###"A string with "# in it. And even "##!"###;
13. println!("{}", delimiter);
15. // 填空
16. let long_delimiter = __;
17. assert_eq!(long_delimiter, "Hello, \"##\"")
18. }
字节字符串
想要一个非 UTF-8 形式的字符串吗(我们之前的 str, &str, String 都是 UTF-8 字符串) ? 可以试试字节字符串或者说字节数组:
示例:
1. use std::str;
3. fn main() {
4. // 注意,这并不是 `&str` 类型了!
5. let bytestring: &[u8; 21] = b"this is a byte string";
7. // 字节数组没有实现 `Display` 特征,因此只能使用 `Debug` 的方式去打印
8. println!("A byte string: {:?}", bytestring);
10. // 字节数组也可以使用转义
11. let escaped = b"\x52\x75\x73\x74 as bytes";
12. // ...但是不支持 unicode 转义
13. // let escaped = b"\u{211D} is not allowed";
14. println!("Some escaped bytes: {:?}", escaped);
16. // raw string
17. let raw_bytestring = br"\u{211D} is not escaped here";
18. println!("{:?}", raw_bytestring);
20. // 将字节数组转成 `str` 类型可能会失败
21. if let Ok(my_str) = str::from_utf8(raw_bytestring) {
22. println!("And the same as text: '{}'", my_str);
23. }
25. let _quotes = br#"You can also use "fancier" formatting, \
26. like with normal raw strings"#;
28. // 字节数组可以不是 UTF-8 格式
29. let shift_jis = b"\x82\xe6\x82\xa8\x82\xb1\x82\xbb"; // "ようこそ" in SHIFT-JIS
31. // 但是它们未必能转换成 `str` 类型
32. match str::from_utf8(shift_jis) {
33. Ok(my_str) => println!("Conversion successful: '{}'", my_str),
34. Err(e) => println!("Conversion failed: {:?}", e),
35. };
36. }
如果大家想要了解更多关于字符串字面量、转义字符的话,可以看看 Rust Reference 的 ‘Tokens’ 章节.
字符串索引string index
- 🌟🌟 你无法通过索引的方式去访问字符串中的某个字符,但是可以使用切片的方式
&s1[start..end],但是start和end必须准确落在字符的边界处.
2. fn main() {
3. let s1 = String::from("hi,中国");
4. let h = s1[0]; // 修改当前行来修复错误,提示: `h` 字符在 UTF-8 格式中只需要 1 个字节来表示
5. assert_eq!(h, "h");
7. let h1 = &s1[3..5];// 修改当前行来修复错误,提示: `中` 字符在 UTF-8 格式中需要 3 个字节来表示
8. assert_eq!(h1, "中");
9. }
操作 UTF-8 字符串
- 🌟
2. fn main() {
3. // 填空,打印出 "你好,世界" 中的每一个字符
4. for c in "你好,世界".__ {
5. println!("{}", c)
6. }
7. }
utf8_slice
我们可以使用三方库 utf8_slice 来访问 UTF-8 字符串的某个子串,但是与之前不同的是,该库索引的是字符,而不是字节.
Example
1. use utf8_slice;
2. fn main() {
3. let s = "The 🚀 goes to the 🌑!";
5. let rocket = utf8_slice::slice(s, 4, 5);
6. // 结果是 "🚀"
7. }
你可以在这里找到答案(在 solutions 路径下)
