move occurs because `self.name` has type `std::string::String`, which does not implement the `Copy`

 

cat src/main.rs 
#[derive(Debug)]
struct f_closure{
        name: String,
}
impl f_closure{
        fn fn_call(& self) -> String{
                        self.name
        }
}
fn main() {
    let name = String::from("kobe");
        let kobe = f_closure{name:name,};
        println!("name {}",kobe.fn_call());
}

 

 

[root@bogon f_closure]# cargo build
   Compiling own v0.1.0 (/data2/rust/f_closure)
warning: type `f_closure` should have an upper camel case name
 --> src/main.rs:2:8
  |
2 | struct f_closure{
  |        ^^^^^^^^^ help: convert the identifier to upper camel case: `FClosure`
  |
  = note: `#[warn(non_camel_case_types)]` on by default

error[E0507]: cannot move out of `self.name` which is behind a shared reference
 --> src/main.rs:7:4
  |
7 |             self.name
  |             ^^^^^^^^^ move occurs because `self.name` has type `std::string::String`, which does not implement the `Copy` trait

error: aborting due to previous error; 1 warning emitted

For more information about this error, try `rustc --explain E0507`.

 

cat  src/main.rs 
#[derive(Debug)]
struct f_closure{
        name: String,
}
impl f_closure{
        fn fn_call( self) -> String{  //不是&self
                        self.name
        }
}
fn main() {
    let name = String::from("kobe");
        let kobe = f_closure{name:name,};
        println!("name {}",kobe.fn_call());
}

 

[root@bogon f_closure]# cargo build
warning: type `f_closure` should have an upper camel case name
 --> src/main.rs:2:8
  |
2 | struct f_closure{
  |        ^^^^^^^^^ help: convert the identifier to upper camel case: `FClosure`
  |
  = note: `#[warn(non_camel_case_types)]` on by default

warning: 1 warning emitted

    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
[root@bogon f_closure]# cargo run
warning: type `f_closure` should have an upper camel case name
 --> src/main.rs:2:8
  |
2 | struct f_closure{
  |        ^^^^^^^^^ help: convert the identifier to upper camel case: `FClosure`
  |
  = note: `#[warn(non_camel_case_types)]` on by default

warning: 1 warning emitted

    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/own`
name kobe
[root@bogon f_closure]# 

 

cat  src/main.rs 
#[derive(Debug)]
struct f_closure{
        name: String,
}
impl f_closure{
        fn fn_call( self) -> String{
                        self.name
        }
}
fn main() {
    let name = String::from("kobe");
        let kobe = f_closure{name:name,};
        println!("name {}",kobe.fn_call());
        println!("name {}",kobe.fn_call());
}

 

cargo build
   Compiling own v0.1.0 (/data2/rust/f_closure)
warning: type `f_closure` should have an upper camel case name
 --> src/main.rs:2:8
  |
2 | struct f_closure{
  |        ^^^^^^^^^ help: convert the identifier to upper camel case: `FClosure`
  |
  = note: `#[warn(non_camel_case_types)]` on by default

error[E0382]: use of moved value: `kobe`
  --> src/main.rs:14:21
   |
12 |     let kobe = f_closure{name:name,};
   |         ---- move occurs because `kobe` has type `f_closure`, which does not implement the `Copy` trait
13 |     println!("name {}",kobe.fn_call());
   |                             --------- `kobe` moved due to this method call
14 |     println!("name {}",kobe.fn_call());
   |                        ^^^^ value used here after move
   |
note: this function consumes the receiver `self` by taking ownership of it, which moves `kobe`
  --> src/main.rs:6:14
   |
6  |     fn fn_call( self) -> String{
   |                 ^^^^

error: aborting due to previous error; 1 warning emitted

For more information about this error, try `rustc --explain E0382`.
error: could not compile `own`.

To learn more, run the command again with --verbose.

 

上一篇:371-RpcRrovider分发rpc服务(OnMessage和Closure回调)


下一篇:返回闭包