Rust学习笔记 5.1 常用数据类型

5.1 常用数据类型

String

  • &str: String slices 不可变的

    • let ss = "ss";
      
  • String objects

    • let mut s = String::new();
      
    • let s = String::from("star");
      
    • let s =  "s".to_string();
      
    • 通过format!生成

      let s = format!("{}", 233);
      

String methods

  • len()
  • push 字符
  • push_str 字符串
  • replace

Vector

构造

let mut arr = Vec::new();
let arr = vec![1,2,3];
let arr = vec![1;20];

push 和 remove

arr.push(2);
arr.remove(0);

更新

num[3] = 5;
num.get(3)
fn main() {
    
    
    let mut num: Vec<i32> = vec![0; 20];
    num.push(1);
    match num.get(20) {
    
    
        Some(n) => println!("{}", n),
        None => {
    
    }
    }
}

遍历

  • iter
fn main() {
    
    
    let mut num: Vec<i32> = vec![0; 20];
    num.push(1);
    for it in num.iter() {
    
    
        println!("{}", it);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_51173321/article/details/126033473
5.1