提问人:Abir Sheikh 提问时间:9/19/2023 更新时间:9/19/2023 访问量:37
为什么 rust 允许我们将多重引用存储到不可变变量?[复制]
Why does rust allow us to store mutablre referneces to a immutable variable? [duplicate]
问:
在 rust 中,如果我们可以存储对可变变量的可变引用,如下所示:
fn main() {
let mut str: String = String::new(); // a mutable string
let ref1 = &mut str; // storing a mutable reference to an immutable variable
// ^ ^ but the reference is mutable
// ^ immutable variable
ref1.push_str(" Wait, ref1 was not marked mutable");
// ^ though we marked 'ref1' as immutable, we can mutate its value
println!("{ref1}");
}
为什么允许它?可变引用不应该存储在可变变量中吗?rust
答: 暂无答案
评论