zoukankan      html  css  js  c++  java
  • move occurs because `self.name` has type `std::string::String`, which does not implement the `Copy` trait

    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.
     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,};
            let  f1= || name;
            println!("name {}",f1());
            //println!("name {}",kobe.fn_call());
            //println!("name {}",kobe.fn_call());
    }
    cargo build
       Compiling own v0.1.0 (/data2/rust/f_closure2)
    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: unused variable: `kobe`
      --> src/main.rs:12:6
       |
    12 |     let kobe = f_closure{name:name,};
       |         ^^^^ help: if this is intentional, prefix it with an underscore: `_kobe`
       |
       = note: `#[warn(unused_variables)]` on by default
    
    error[E0382]: use of moved value: `name`
      --> src/main.rs:13:11
       |
    11 |     let name = String::from("kobe");
       |         ---- move occurs because `name` has type `std::string::String`, which does not implement the `Copy` trait
    12 |     let kobe = f_closure{name:name,};
       |                               ---- value moved here
    13 |     let  f1= || name;
       |              ^^ ---- use occurs due to use in closure
       |              |
       |              value used here after move
    
    error: aborting due to previous error; 2 warnings 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.
    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,};
            let  f1= || name;
            println!("name {}",f1());
            println!("name {}",f1());
            //println!("name {}",kobe.fn_call());
            //println!("name {}",kobe.fn_call());
    }
    cargo build
       Compiling own v0.1.0 (/data2/rust/f_closure2)
    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: `f1`
      --> src/main.rs:15:21
       |
    14 |     println!("name {}",f1());
       |                        ---- `f1` moved due to this call
    15 |     println!("name {}",f1());
       |                        ^^ value used here after move
       |
    note: closure cannot be invoked more than once because it moves the variable `name` out of its environment
      --> src/main.rs:13:14
       |
    13 |     let  f1= || name;
       |                 ^^^^
    note: this value implements `FnOnce`, which causes it to be moved when called
      --> src/main.rs:14:21
       |
    14 |     println!("name {}",f1());
       |                        ^^
    
    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.
  • 相关阅读:
    团队绩效打分
    软件对标分析
    目前校园百晓生APP与CSDN软件的对比
    Alpha版
    团队项目第一阶段成果展示
    意见汇总
    团队第一阶段冲刺评价
    冲刺(十)
    【WPF学习】第五十八章 理解逻辑树和可视化树
    【WPF学习】第五十七章 使用代码创建故事板
  • 原文地址:https://www.cnblogs.com/dream397/p/14191919.html
Copyright © 2011-2022 走看看