《Rust权威指南》第8章_通用集合类型_草稿

  • 使用动态数组存储多个值
    • 创建动态数组
    • 更新动态数组
    • 销毁动态数组时也会销毁其中元素
    • 读取动态数组中的元素
    • 遍历动态数组中的值
    • 使用枚举来存储多个类型的值
  • 使用字符串存储UTF-8编码的文本
    • 字符串是什么
    • 创建一个新的字符串
    • 更新字符串
      • 使用+运算符或format!宏来拼接字符串
    • 字符串索引
      • 内部布局
      • 字节、标量值及字形簇!天呐!
    • 字符串切片
    • 遍历字符串的方法
    • 字符串的确没那么简单
  • 在哈希映射中存储键值对
    • 创建一个新的哈希映射
    • 哈希映射与所有权
    • 访问哈希映射中的值
    • 更新哈希映射
      • 更新旧值
      • 只在键没有对应值时插入数据
      • 基于旧值来更新值
    • 哈希函数
  • 总结
use std::borrow::Borrow;

fn main() {
    //动态数组
    let v: Vec<i32> = Vec::new();
    let v = vec![1,2,3];
    let mut v = Vec::new();
    v.push(5);
    v.push(6);
    v.push(7);
    v.push(8);
    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 mut v = vec![1,2,3,4,5];
    //let does_not_exit = &v[100];
    let does_not_exit = v.get(100);
    let first = &v[0];
    //v.push(6);
    println!("The first element is: {}", first);
    let mut v = vec![100,32,57];
    for i in &v {
        println!("{}", i);
    }
    for i in &mut v {
        *i += 50;
    }

    //枚举
    enum SpreadsheetCell {
        Int(i32),
        Float(f64),
        Text(String),
    }
    let row = vec![
        SpreadsheetCell::Int(3),
        SpreadsheetCell::Text(String::from("blue")),
        SpreadsheetCell::Float(10.12),
    ];

    //字符串
    let mut s = String::new();
    let data = "initial contents";
    let s = data.to_string();
    let s = "initial contents".to_string();
    let s = String::from("initial contents");
    let hello = String::from("你好");
    let mut s = String::from("foo");
    s.push_str("bar");
    let mut s1=String::from("foo");
    let s2 = "bar";
    s1.push_str(s2);
    println!("s2 is {}", s2);
    let mut s = String::from("lo");
    s.push('l');
    let s1 = String::from("Hello,");
    let s2 = String::from("world!");
    let s3 = s1 + &s2;//注意这里的s1已经被移动且再也不能被使用了
    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 s1 = String::from("hello");
    //let h = s1[0];

    //哈希映射
    use::std::collections::HashMap;
    let mut scores = HashMap::new();
    scores.insert(String::from("Blue"), 10);
    scores.insert(String::from("Yellow"), 50);
    let teams = vec![String::from("Blue"), String::from("Yellow")];
    let initial_scores = vec![10, 50];
    let scores: HashMap<_, _> =
        teams.iter().zip(initial_scores.iter()).collect();
    let field_name = String::from("Favorite color");
    let field_value = String::from("Blue");
    let mut map = HashMap::new();
    map.insert(field_name,field_value);
    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);
    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);
    }
    let mut scores = HashMap::new();
    scores.insert(String::from("Blue"), 10);
    scores.insert(String::from("Blue"), 25);
    println!("{:?}", scores);
    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);
    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);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值