首页 > 其他 > 详细

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

时间:2020-12-26 18:02:52      阅读:138      评论:0      收藏:0      [点我收藏+]

 

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.

 

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

原文:https://www.cnblogs.com/dream397/p/14191919.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!