相关疑难解决方法(0)

参数类型"T"的寿命可能不够长

我正在尝试在Rust写一个小程序,但我无法让它工作.

我在一个较小的脚本中重现了错误:

fn main() {
    let name = String::from("World");
    let test = simple(name);
    println!("Hello {}!", test())
}

fn simple<T>(a: T) -> Box<Fn() -> T> {
    Box::new(move || -> T {
        a
    })
}
Run Code Online (Sandbox Code Playgroud)

当我编译它时,我收到此错误:

error[E0310]: the parameter type `T` may not live long enough
  --> test.rs:8:9
   |
7  |       fn simple<T>(a: T) -> Box<Fn() -> T> {
   |                 - help: consider adding an explicit lifetime bound `T: 'static`...
8  | /         Box::new(move || -> T {
9  | |             a …
Run Code Online (Sandbox Code Playgroud)

generics closures rust

2
推荐指数
1
解决办法
600
查看次数

标签 统计

closures ×1

generics ×1

rust ×1