Code Fix

上級E0507: cannot move out of ... which is behind a shared reference › パターン4

E0507: cannot move out of ... which is behind a shared reference

&で借りている構造体のフィールドを、所有権を要求する関数にそのまま渡そうとするとE0507エラーになります。

 1  struct Record { label: String }
 2  fn take(s: String) -> usize { s.len() }
 3  fn main() {
 4      let r = Record { label: String::from("note") };
 5      let rref = &r;
 6      let len = take(_____________________);
                                 ^
 7      println!("{}", len);
 8  }
error[E0507]: cannot move out of `rref.label` which is behind a shared reference

空欄を埋めて実行を通す

広告
広告スロット(未設定)

E0507: cannot move out of ... which is behind a shared reference

参照の向こう側にある値は借りているだけなので、勝手に持ち出す(ムーブする)ことはできません。

このエラーの解説と類題をまとめて見る →

広告
広告スロット(未設定)