< 返回版块

祐祐 发表于 2021-05-10 12:04

Tags:闭包,所有权

我错在哪里😥?。

fn main_FnOnce() {
    fn fn_immut<F>(f: F) where F: FnOnce() -> String {
        println!("run {}", f());
    }
    fn fn_immutb<F>(f: F) where F: for<'a> Fn() -> &'a String {
        println!("run {}", f());
    }
    let a = "Fn".to_string();
    let b = &"Fn".to_string();
    let f = || a;
    let fb = || b;
    fn_immut(f);
    fn_immutb(fb);
}
error[E0261]: use of undeclared lifetime name `'a`
  --> src\main.rs:56:21
   |
56 |         F: Fn() -> &'a String,
   |                     ^^ undeclared lifetime
   |
   = note: for more information on higher-ranked polymorphism, visit https://doc.rust-lang.org/nomicon/hrtb.html
help: consider introducing lifetime `'a` here
   |
54 |     fn fn_immutb<'a, F>(f: F)
   |                  ^^^
help: consider making the bound lifetime-generic with a new `'a` lifetime
   |
56 |         F: for<'a> Fn() -> &'a String,
   |            ^^^^^^^

error: aborting due to previous error; 1 warning emitted

评论区

写评论
作者 祐祐 2021-06-09 07:48

对不起喔问完后上来 字体主题: Cascadia Code

--
👇
leslieDD: 可以贴一下你用的主题么

leslieDD 2021-05-14 17:54

可以贴一下你用的主题么

leslieDD 2021-05-14 17:26

请问你用的是什么字体啊,可以贴一下么~~~

作者 祐祐 2021-05-10 23:56

感谢,已解决。

--
👇
ZIP97: 没有给 'a 添加泛型声明

fn main() {
    let a = "Fn".to_string();
    let b = &"Fn".to_string();
    let f = || a;
    let fb = || b;

    fn_immut(f);
    fn_immutb(fb);
}

fn fn_immut<F>(f: F)
    where F: FnOnce() -> String {
    println!("run {}", f());
}

fn fn_immutb<'a, F>(f: F)
    where F: Fn() -> &'a String {
    println!("run {}", f());
}
苦瓜小仔 2021-05-10 14:19

没有给 'a 添加泛型声明

fn main() {
    let a = "Fn".to_string();
    let b = &"Fn".to_string();
    let f = || a;
    let fb = || b;

    fn_immut(f);
    fn_immutb(fb);
}

fn fn_immut<F>(f: F)
    where F: FnOnce() -> String {
    println!("run {}", f());
}

fn fn_immutb<'a, F>(f: F)
    where F: Fn() -> &'a String {
    println!("run {}", f());
}
Mike Tang 2021-05-10 13:43

终端里编译打印的信息发上来。

作者 祐祐 2021-05-10 12:55

救救出学者。

--
👇
Mike Tang: 编译错误信息来一个

Mike Tang 2021-05-10 12:22

编译错误信息来一个

1 共 8 条评论, 1 页