zoukankan      html  css  js  c++  java
  • cannot borrow as mutable

    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));
    
    }
  • 相关阅读:
    【Tools__技巧】软件技巧整理归纳
    【JS__框架】主页面F5刷新iframe框架页面
    为类型增加选择属性
    单元测试mock当前时间
    silverlight属性改变事件通知
    修改博客园推荐人数增加W效果
    Enum扩展特性,代替中文属性
    Python爬虫-萌妹子图片
    吐槽下魅族
    使用openXML 不用插件导出excel
  • 原文地址:https://www.cnblogs.com/dream397/p/14192035.html
Copyright © 2011-2022 走看看