上級 › E0507: cannot move out of ... which is behind a shared reference › パターン3
E0507: cannot move out of ... which is behind a shared reference
&で借りている構造体のフィールドを、所有権を要求する関数にそのまま渡そうとするとE0507エラーになります。
1 struct Box3 { content: String } 2 fn take(s: String) -> usize { s.len() } 3 fn main() { 4 let b = Box3 { content: String::from("item") }; 5 let bref = &b; 6 let len = take(_______________________); ^ 7 println!("{}", len); 8 }
error[E0507]: cannot move out of `bref.content` which is behind a shared reference
空欄を埋めて実行を通す
フィールドの値を借用元の外に持ち出したい場合は、cloneで複製してから渡す必要があります。
次の問題