欢迎光临
  • 文章
  • 工具
  • 随记
  • 导航
  • 分类
  • 关于我
首页
菜单
    Rust学习笔记
    日志记录

    Rust学习笔记

    2024-08-19
    111
    0

    Cargo 换源

    第一种: 编辑 .cargo/config.toml
    [source.crates-io]
    replace-with = "ustc"
    
    [source.ustc]
    registry = "sparse+https://mirrors.ustc.edu.cn/crates.io-index/"
    
    [net]
    git-fetch-with-cli = true
    第二种:
    export RUSTUP_DIST_SERVER="https://rsproxy.cn"
    export RUSTUP_UPDATE_ROOT="https://rsproxy.cn/rustup"
    
    ## ~/.cargo/config
    [source.crates-io]
    replace-with = 'rsproxy-sparse'
    [source.rsproxy]
    registry = "https://rsproxy.cn/crates.io-index"
    [source.rsproxy-sparse]
    registry = "sparse+https://rsproxy.cn/index/"
    [registries.rsproxy]
    index = "https://rsproxy.cn/crates.io-index"
    [net]
    git-fetch-with-cli = true

    打包

    cargo install cross
    cross build --release --target x86_64-pc-windows-gnu
    cross build --release --target x86_64-apple-darwin
    cross build --release --target x86_64-unknown-linux-gnu

    配置

    应对panic 错误处理

    image-20240724074057138
    
    [profile.release]
    panic = "abort"
    获得产生panic的回溯信息:
    添加环境变量:RUST_BACKTRACE=1 | RUST_BACKTRACE=full

    类型部分

    image-20240722082800271
    
    image-20240722082850789
    
    image-20240722083045653
    
    Tuple例子:
    image-20240722083118578
    
    访问Tuple:
    image-20240722083231575
    
    image-20240722083333032
    

    遍历数组

    let a = [10, 20, 30, 40, 50]
    for element in a.iter() {
    println!("value: {}", element)
    }

    所有权 Stack、Heap

    在一个作用域内变量用完rust会自动drop(删除),将内存自动交还给操作系统。
    只有在Heap上才区分深拷贝和浅拷贝。
    move:转移所有权(Rust设计原则),旧的不能再使用
    copy:将Stack,包括Heap里面的数据Copy一份(自然旧的还可以用)

    Stack的数据

    image-20240722093632981
    
    image-20240722093801982
    

    函数与所有权

    函数传参时会取得参数的所有权,在函数返回时失效
    image-20240722095818266
    
    如何让函数使用某个值,不获得所有权:
    麻烦的做法:
    let (s2, len) = get_length(s1)
    
    fn get_length(s: String) -> (String, unsize) {
    let length = s.len();
    (s, length) //返回元组
    }

    引用与借用

    &变量 &mut 变量

    Struct 结构体

    image-20240723061853823
    

    Tuple struct

    image-20240723061959305
    

    打印 Struct

    image-20240723063343579
    
    image-20240723063506997
    

    Struct 定义方法

    image-20240723064139363
    

    Struct 关联函数

    image-20240723064510572
    
    image-20240723064438300
    

    枚举

    枚举也可以定义方法,和struct定义方法一样
    image-20240723064813617
    
    image-20240723065642365
    

    Rust中 “Null” 的概念

    image-20240723070138703
    
    image-20240723070218217
    
    image-20240723070723129
    

    Match

    image-20240723071122349
    
    匹配Option时,必须写完Some、None,
    如果不想每个都匹配,可以使用_通配符,如:
    let a = 3u8;
    
    match a {
    1 => println!("111"),
    2 => println!("222"),
    _ => println!("???")
    }

    常用的集合

    Vector

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

    String

    拼接字符串:
    format()

    ? 运算符

    image-20240724082804686
    

    泛型

    方法中的泛型
    image-20240724090010344
    
    Rust学习笔记
    温小白
    https://www.youngwen.com/post/30
    本站文章除特别声明外,均采用BY-NC-SA许可协议。转载请注明出处!
    上一篇
    QQ 出现 “当前网页非官方页面” 申诉解决办法
    下一篇
    SQL Server学习笔记
    登录以开始回复
    暂无回复。
    目录