上級 › E0507: cannot move out of ... which is behind a shared reference › パターン2
E0507: cannot move out of ... which is behind a shared reference
&で借りている構造体のフィールドを、所有権を要求する関数にそのまま渡そうとするとE0507エラーになります。
1 struct Holder { text: String } 2 fn take(s: String) -> usize { s.len() } 3 fn main() { 4 let h = Holder { text: String::from("hello") }; 5 let href = &h; 6 let len = take(____________________); ^ 7 println!("{}", len); 8 }
error[E0507]: cannot move out of `href.text` which is behind a shared reference
空欄を埋めて実行を通す
共有参照の向こうにあるフィールドをムーブすると、参照元の構造体が不完全な状態になるため禁止されています。
次の問題