Rust - Basic - 08 - Collections
Collections
Comprehension
vector
// 声明方式 1 - 不可变、new、指定类型 let v: Vec<i32> = Vec::new(); // 声明方式 2 - 使用 macro let v = vec![1, 2, 3]; // 声明方式 3 - 可变、new、后续 push 时自动指定类型 let mut v = Vec::new(); v.push(5); // 获取元素 let v = vec![1, 2, 3, 4, 5]; // 如果越界就 panic let third: &i32 = &v[2]; println!("The third element is {}", third); // 如果越界则可以在 None 分支处理 match v.get(2) { Some(third) => println!("The third element is {}", third), None => println!("There is no third element."), } // 遍历 let v = vec![100, 32, 57]; for i in &v { println!("{}", i); } // 可变遍历 let mut v = vec![100, 32, 57]; for i in &mut v { *i += 50; } // 虽然 Vector 只能支持一种类型,但是可以通过 enum 来扩展 enum SpreadsheetCell { Int(i32), Float(f64), Text(String), } let row = vec![ SpreadsheetCell::Int(3), SpreadsheetCell::Text(String::from("blue")), SpreadsheetCell::Float(10.12), ];
string
// 初始化方式 1 - new let mut s = String::new(); // 初始化方式 2 - to_string let data = "initial contents"; let s = data.to_string(); // 拼接方式 1 - push_str s.push_str("bar"); // 拼接方式 2 - push s.push('l'); // 拼接方式 3 - + let s1 = String::from("Hello, "); let s2 = String::from("world!"); let s3 = s1 + &s2; // s1 被 move // 拼接方式 4 - format! let s1 = String::from("tic"); let s2 = String::from("tac"); let s3 = String::from("toe"); let s = format!("{}-{}-{}", s1, s2, s3); // string 在 Rust 里有三种表现形式 "नमस्ते" // Bytes 字节、 Scalar Values 标量值、 Grapheme Clusters 字素。依次表现如下 [224, 164, 168, 224, 164, 174, 224, 164, 184, 224, 165, 141, 224, 164, 164, 224, 165, 135] // for b in "नमस्ते".bytes() ['न', 'म', 'स', '्', 'त', 'े'] // for b in "नमस्ते".chars() ["न", "म", "स्", "ते"] // 标准库没有提供可用于 for-in 迭代函数 // 取 string 部分值的时候,无法直接用 index 获取 // let s1 = String::from("hello"); let h = s1[0]; // 可以用 slice let hello = "Здравствуйте"; // 这里每个字素都需要 2 个 Bytes 存储 let s = &hello[0..4]; // 但需要注意 range 范围合法,这里如果是 [0..1] 则会运行时 panic
hash map
// 初始化 let mut scores = HashMap::new(); // 新增/覆盖值 scores.insert(String::from("Blue"), 10); scores.insert(String::from("Yellow"), 50); // 获取值 let score = scores.get(&color); // for-in for (key, value) in &scores { println!("{}: {}", key, value); } // 不存在则新增 scores.entry(String::from("Yellow")).or_insert(50); scores.entry(String::from("Blue")).or_insert(50);
Origin
https://doc.rust-lang.org/std/collections/index.html https://doc.rust-lang.org/book/ch08-00-common-collections.html
…
Methods for Iterating Over Strings
Fortunately, you can access elements in a string in other ways.
If you need to perform operations on individual Unicode scalar values, the best way to do so is to use the chars
method. Calling chars
on “नमस्ते” separates out and returns six values of type char
, and you can iterate over the result to access each element:
for c in "नमस्ते".chars() {
println!("{}", c);
}
This code will print the following:
न
म
स
्
त
े
The bytes
method returns each raw byte, which might be appropriate for your domain:
for b in "नमस्ते".bytes() {
println!("{}", b);
}
This code will print the 18 bytes that make up this String
:
224
164
// --snip--
165
135
But be sure to remember that valid Unicode scalar values may be made up of more than 1 byte.
Getting grapheme clusters from strings is complex, so this functionality is not provided by the standard library. Crates are available on crates.io if this is the functionality you need.