Sparetire
V2EX  ›  Rust

Rust 中说的不可变指的是什么?

  •  
  •   Sparetire · Jan 11, 2019 · 4623 views
    This topic created in 2681 days ago, the information mentioned may be changed or developed.

    RT, Rust 中说变量默认是不可变的, 这里的不可变, 是指绑定不可变, 还是指值不可变呢? 或两者都有?

    这块的表述可能不是很准确, 只在大学学过 C, 其实我就是想问, 这个是类似于 int const* p 还是类似于 int* const p 这样的?

    2 replies    2019-01-15 22:44:10 +08:00
    quinoa42
        1
    quinoa42  
       Jan 14, 2019
    除去极其个别的情况(你后面会学到),如果要修改函数的某个参数或是变量,需要要么 own 那个值,要么有 mutable reference
    reference 实际上相当于是 const pointer,不能修改指向的地址位置的(用 Rustacean 的话来讲,你只是 borrow 了这个变量)

    也就是说是:&bla == ??? const *const bla
    &mut bla == ??? * const bla
    mut bla = ??? bla
    bla = ??? const bla

    我也是前段时间才开始学 rust,如有错误请多多指正
    Sparetire
        2
    Sparetire  
    OP
       Jan 15, 2019   ❤️ 1
    @quinoa42 我的理解是类似于 const int * const p 这样的, 即指向不可变, 值也不可变...
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2969 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 04:57 · PVG 12:57 · LAX 21:57 · JFK 00:57
    ♥ Do have faith in what you're doing.