🚩 Home / rust / std_lib.md

Option

  • 定义:
    pub enum Option<T> {
      None,
      Some(T),
    }

Vector

  • 创建

    let v: Vec<i32> = Vec::new();
  • 初始化,一般使用vec!宏来初始化

    let v = vec![1,2,3];
  • push

    let mut v = Vec::new();
    v.push(1);
    v.push(2); // type Vec<i32>
  • read:两种方式1.index,2.get 方法

    fn main() {
      let arr = vec![1,2,3];
      let a0 = arr[0];
      println!("{}", a0);
      let a1 = match arr.get(3) {
          Some(a) => {a},
          None => {&{0}}
      };
      println!("{}", a1);
    }
  • 遍历

    fn main() {
      let v = vec![100, 32, 57];
      for i in &v {
          println!("{}", i);
      }
    }
  • 通过enum存不同类型数据

    #[derive(Debug)]
    enum SpreadsheetCell {
      Int(i32),
      Float(f64),
      Text(String),
    }
    fn main() {
      let row = vec![
          SpreadsheetCell::Int(3),
          SpreadsheetCell::Text(String::from("blue")),
          SpreadsheetCell::Float(10.12),
      ];
    
      println!("{:?}", row);
    }

String

Hash Map

  • 创建
    use std::collections::HashMap;
    

fn main() { let mut scores = HashMap::new();

scores.insert(String::from("Blue"), 10);
scores.insert(String::from("Yellow"), 50);
println!("{:?}", scores);

}


```rust
use std::collections::HashMap;

    let teams = vec![String::from("Blue"), String::from("Yellow")];
    let initial_scores = vec![10, 50];

    let mut scores: HashMap<_, _> =
        teams.into_iter().zip(initial_scores.into_iter()).collect();
  • 获取
    use std::collections::HashMap;
    

fn main() { let mut scores = HashMap::new();

// scores.insert(String::from("Blue"), 10);
// scores.insert(String::from("Yellow"), 50);
scores.insert(10, "aaa");
scores.insert(20, "bbb");

let team_name = 10;
let score = scores.get(&team_name);
println!("{:?}", score);

}


- 遍历
```rust
use std::collections::HashMap;

fn main() {
    let mut scores = HashMap::new();

    scores.insert(10, "aaa");
    scores.insert(20, "bbb");

    for (key, val) in &scores {
        println!("{} {}", key, val);
    }
}
  • 更新

    • 覆写

      use std::collections::HashMap;
      
      fn main() {
        let mut scores = HashMap::new();
      
        scores.insert(String::from("Blue"), 10);
        scores.insert(String::from("Blue"), 25);
      
        println!("{:?}", scores);
      }
    • key不存在时插入,存在不插入

      use std::collections::HashMap;
      
      fn main() {
        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); // {"Blue": 10, "Yellow": 50}
      }
    • 在原值基础上修改

      use std::collections::HashMap;
      fn main() {
        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);
      }