Closed
Description
Code example:
#[derive(Debug)]
struct Str {
x: Option<i32>,
}
fn main() {
let s = Str { x: None };
let sr = &s;
let mut sm = sr.clone();
foo(&mut sm.x);
}
fn foo(s: &mut Option<i32>) {
if s.is_none() {
*s = Some(0);
}
println!("{:?}", s);
}
The Error is:
error: cannot borrow immutable field `sm.x` as mutable
--> <anon>:10:14
|>
10 |> foo(&mut sm.x);
|> ^^^^
This is misleading because the fix is to add a Clone
impl for Str
Meta
rustc --version --verbose
:
rustc 1.9.0-nightly (d5a91e6 2016-03-26)
binary: rustc
commit-hash: d5a91e6
commit-date: 2016-03-26
host: x86_64-apple-darwin
release: 1.9.0-nightly