use std::collections::HashMap;
fn print_type_of<T>(_: &T) {
print!("Type is : {} \n", std::any::type_name::<T>());
}
// vector 只能储存相同类型的值,这很不方便使用。Rust 中有枚举可以帮助 vector 来存储不同类型的数据。
enum Data {
Int(i32),
Double(f64),
Str(String),
}
fn main() {
println!("----------------Vector----------------");
let mut v1: Vec<i32> = Vec::new();
v1.push(1);
v1.push(2);
v1.push(3);
v1.push(4);
let mut v2 = vec![5, 6, 7, 8];
v2.push(9);
v2.push(10);
for v in &v1 {
print!("{} \n", v);
}
println!("");
for v in &v2 {
print!("{} \n", v);
}
println!("");
print_type_of(&v1);
print_type_of(&v2);
let third: &i32 = &v1[2];
println!("The third element is {}", third);
match v2.get(2) {
Some(third) => println!("The third element is {}", third),
None => println!("There is no third element."),
}
println!("");
let mut vec: Vec<Data> = Vec::new();
vec.push(Data::Int(99));
vec.push(Data::Double(99.99));
vec.push(Data::Str(String::from("Hello Rust")));
for index in &vec {
match index {
Data::Int(value) => println!("{}", value),
Data::Double(value) => println!("{}", value),
Data::Str(value) => println!("{}", value),
}
}
println!("");
let vv = vec![
Data::Int(99),
Data::Double(99.99),
Data::Str(String::from("Hello Rust")),
];
for index in &vv {
match index {
Data::Int(value) => println!("{}", value),
Data::Double(value) => println!("{}", value),
Data::Str(value) => println!("{}", value),
}
}
println!("----------------String----------------");
let s1: String = String::from("hello Str1");
let str1: &str = "hello str1";
let str2 = &s1;
print_type_of(&s1); // Type is: alloc::string::String
print_type_of(&str1); // Type is: &str
print_type_of(&str2); // Type is: &alloc::string::String
let i = 10;
print_type_of(&i);
let mut s = String::from("Hello ");
s.push('r');
println!("追加字符 push() -> {}", s);
s.push_str("ust!");
println!("追加字符串 push_str() -> {}", s);
let mut s = String::from("Hello rust!");
s.insert(5, ',');
println!("插入字符 insert() -> {}", s);
s.insert_str(6, " I like");
println!("插入字符串 insert_str() -> {}", s);
//拼接
//在使用 +, 必须传递切片引用类型。不能直接传递 String 类型
let string_append = String::from("hello ");
let string_rust = String::from("rust");
// &string_rust会自动解引用为&str
let result = string_append + &string_rust;
let mut result = result + "!";
result += "!!!";
println!("连接字符串 + -> {}", result);
//使用 format! 连接字符串
let s1 = "hello";
let s2 = String::from("rust");
let s = format!("{} {}!", s1, s2);
println!("{}", s);
println!("----------------HashMap----------------");
let mut map = HashMap::new();
map.insert(String::from("Blue"), 10);
map.insert(String::from("Yellow"), 50);
//用循环遍历HashMap
for iter in &map {
println!("{}:{}", iter.0, iter.1);
}
println!("");
//用循环遍历HashMap,模式匹配
for (k, v) in &map {
println!("{}:{}", k, v);
}
println!("");
//用 get(key) 方法获取 value
match map.get("Blue") {
Some(val) => println!("Blue:{}\n", val),
None => println!("No this value"),
};
println!("{:?} \n", map);
//覆盖一个值
map.insert(String::from("Blue"), 25);
//只在map中没有对应值时才插入
map.entry("Blue".to_string()).or_insert(3); //不会插入
map.entry("Red".to_string()).or_insert(4); //会插入
println!("{:#?} \n", map);
//获取key=Red对应的值的可变引用,并修改其值
let vaule = map.entry(String::from("Red")).or_insert(55);
*vaule = 66;
//可变迭代器
for (key, value) in map.iter_mut() {
println!("=>key:{:?} value :{:?}", key, value);
*value += 1;
}
println!("{:#?} \n", map);
let mut map = HashMap::new();
map.insert("color", "red");
map.insert("size", "10 m^2");
map.insert("a", "aba");
// 在已经确定有某个键的情况下如果想直接修改对应的值,有更快的办法:
if let Some(x) = map.get_mut("a") {
*x = "b";
} //当使用 insert 方法添加新的键值对的时候,如果已经存在相同的键,会直接覆盖对应的值。如果你想"安全地插入",就是在确认当前不存在某个键时才执行的插入动作
map.entry("color").or_insert("red1");
println!("映射");
//映射表支持迭代器
for p in map.iter() {
println!("{:?}", p);
}
// println!("{}", map.get("color2").unwrap()); // panicked at 'called`Option::unwrap()` on a `None` value' 在错误处理讲解
}