t_wの輪郭

Feedlyでフォローするボタン

Copy特質を持たない変数関数に渡すと、所有権の移動が発生し、関数に渡した変数は使用不可能になる。

fn main() {
    let s = String::from("hello");
    test(s);
    println!("{}", s);   //error
    /*
    error[E0382]: borrow of moved value: `s`
    --> src\main.rs:4:20
    |
    2 |     let s = String::from("hello");
    |         - move occurs because `s` has type `String`, which does not implement the `Copy` trait
    3 |     test(s);
    |          - value moved here
    4 |     println!("{}", s);   //error
    |                    ^ value borrowed here after move
    */
}

fn test(s:String) {
    println!("{}", s);   //hello
}

Copy特質を持たない変数をコピーすると、所有権の移動が発生し、コピー元の変数は使用が不可能になる。

let s1 = String::from("hello");
let s2 = s1;
println!("{}", s1);   //error
/*
error[E0382]: borrow of moved value: `s1`
 --> src\main.rs:5:16
  |
3 | let s1 = String::from("hello");
  |     -- move occurs because `s1` has type `String`, which does not implement the `Copy` trait
4 | let s2 = s1;
  |          -- value moved here
5 | println!("{}", s1);   //hello
  |                ^^ value borrowed here after move
*/

ベクタの値を走査する時にベクタに参照(&)を使う理由は、所有権の移動が発生し、走査して以降ベクタが利用できなくなるからである。

参照を使わずに所有権の移動する例

let v = vec![100, 32, 57];
for i in v {
    println!("{}", i);
}

for i in v {
    println!("{}", i);
}
error[E0382]: use of moved value: `v`
   --> src\main.rs:8:10
    |
3   | let v = vec![100, 32, 57];
    |     - move occurs because `v` has type `Vec<i32>`, which does not implement the `Copy` trait
4   | for i in v {
    |          -
    |          |
    |          `v` moved due to this implicit call to `.into_iter()`
    |          help: consider borrowing to avoid moving into the for loop: `&v`
...
8   | for i in v {
    |          ^ value used here after move
    |