vector:
允许我们一个挨着一个地储存一系列数量可变的值 字符串(string)是一个字符的集合
String 类型,不过在本章我们将深 入了解
哈希 map(hash map):
允许我们将值与一个特定的键(key)相关联。这是一个叫做 map 的更通用的数据结构的特定实现。
vector
for (index,container) in self.containers.iter().enumerate() {
for (i, conf) in container
.config
.clone()
.unwrap_or_else(|| vec![])
.iter()
.enumerate()
{
let mut values = BTreeMap::new();
//config name is file path
let filename = Path::new(conf.path.as_str())
.file_name()
.unwrap()
.to_os_string()
.into_string()
.expect("config file name");
values.insert(filename, conf.resolve_value(resolved_vals.clone()));
// make sure configmap name global unique in the namespace
// multiply containers in one component and multiply configmap in on container
// configmap name: instance_name + "-" + contrainer_index + "-" + config index
let config_map_name = utils::concat_str(instance_name.clone(),index.to_string(),i.to_string());
configs.insert(config_map_name.clone(), values);
}
}
# create new vector
let v: Vec<i32> = Vec::new();
# create and initial
let v = vec![1, 2, 3];
# add new item
let mut v = Vec::new();
v.push(5);
v.push(6);
v.push(7);
v.push(8);
# read
let v = vec![1, 2, 3, 4, 5];
let third: &i32 = &v[2];
println!("The third element is {}", third);
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);
}
遍历2
# 给每个元素+50
let mut v = vec![100, 32, 57];
for i in &mut v {
*i += 50;
}
遍历
let v = vec!['a', 'b', 'c'];
for (index, value) in v.iter().enumerate() {
println!("{} is at index {}",
value, index);
}
使用枚举来储存多种类型
enum SpreadsheetCell {
Int(i32),
Float(f64),
Text(String),
}
let row = vec![
SpreadsheetCell::Int(3),
SpreadsheetCell::Text(String::from("blue")),
SpreadsheetCell::Float(10.12),
];
string
let mut s = String::new();
let data = "initial contents"; let s = data.to_string();
// 该方法也可直接用于字符串字面值:
let s = "initial contents".to_string();
# 可以通过 push_str 方法来附加字符串 slice,从而使 String 变长
let mut s = String::from("foo");
s.push_str("bar");
let s1 = String::from("Hello, ");
let s2 = String::from("world!");
let s3 = s1 + &s2; // 注意 s1 被移动了,不能继续使用
这个语句会获 取 s1 的所有权,附加上从 s2 中拷贝的内容,并返回结果的所有权赋给s3
所以执行完, s1失效, s2和s3继续使用
let s1 = String::from("tic");
let s2 = String::from("tac");
let s3 = String::from("toe");
let s = s1 + "-" + &s2 + "-" + &s3;
# 等同
let s1 = String::from("tic");
let s2 = String::from("tac");
let s3 = String::from("toe");
let s = format!("{}-{}-{}", s1, s2, s3);
长度
let len = String::from("Hola").len();
HashMap
use std::collections::HashMap;
let mut scores = HashMap::new();
scores.insert(String::from("Blue"), 10);
scores.insert(String::from("Yellow"), 50);
访问
use std::collections::HashMap;
let mut scores = HashMap::new();
scores.insert(String::from("Blue"), 10);
scores.insert(String::from("Yellow"), 50);
let team_name = String::from("Blue");
let score = scores.get(&team_name);
遍历
use std::collections::HashMap;
let mut scores = HashMap::new();
scores.insert(String::from("Blue"), 10); scores.insert(String::from("Yellow"), 50);
for (key, value) in &scores {
println!("{}: {}", key, value);
}
检查key是否存在
use std::collections::HashMap;
let mut scores = HashMap::new();
scores.insert(String::from("Blue"), 10);
scores.entry(String::from("Yellow")).or_insert(50);
scores.entry(String::from("Blue")).or_insert(50); println!("{:?}", scores);
根据已存在值更新
use std::collections::HashMap;
let text = "hello world wonderful world";
let mut map = HashMap::new();
for word in text.split_whitespace() {
let count = map.entry(word).or_insert(0);
*count += 1;
}
println!("{:?}", map);
网友评论