ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2020-12-26 11:33:10  阅读:449  来源: 互联网

标签:std closure because name self kobe main fn


 

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.

 

标签:std,closure,because,name,self,kobe,main,fn
来源: https://www.cnblogs.com/dream397/p/14191919.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有