首页 > 其他 > 详细

cannot borrow as mutable

时间:2020-12-26 17:43:27      阅读:53      评论:0      收藏:0      [点我收藏+]

 

 

cat src/main.rs 
#[derive(Debug)]
struct f_closure{
        name: String,
}
impl f_closure{
        fn fn_call( self) -> String{
                        self.name
        }
}
fn get_string<T>(name: String , mut f: T) -> String where T : FnMut(String) -> String{
                f(name)
        }
fn main() {
                        let name = String::from("kobe");
                        let  f1= |x : String | -> String {
                        name.push_str("24");
                        format!("{}+ {}",x, name)
        };
    let name2 = String::from("dirk");
        println!("name2 {}",get_string(name2, f1));

}

 

 

cargo build
   Compiling own v0.1.0 (/data2/rust/fnmut)
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[E0596]: cannot borrow `name` as mutable, as it is not declared as mutable
  --> src/main.rs:16:7
   |
14 |             let name = String::from("kobe");
   |                 ---- help: consider changing this to be mutable: `mut name`
15 |             let  f1= |x : String | -> String {
16 |             name.push_str("24");
   |             ^^^^ cannot borrow as mutable

error: aborting due to previous error; 1 warning emitted

For more information about this error, try `rustc --explain E0596`.
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 get_string<T>(name: String , mut f: T) -> String where T : FnMut(String) -> String{
                f(name)
        }
fn main() {
                        let mut name = String::from("kobe");
                        let  f1= |x : String | -> String {
                    name.push_str("24");
                        format!("{}+ {}",x, name)
        };
    let name2 = String::from("dirk");
        println!("name2 {}",get_string(name2, f1));

}

 

cannot borrow as mutable

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

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