Rust 你能表达';在返回值的生存期内有效';在生命周期系统中?

Rust 你能表达';在返回值的生存期内有效';在生命周期系统中?,rust,Rust,这是我多次遇到的问题 您有需要转到任务的数据 您希望安全地将数据发送到任务并远程处理它 …而你需要等待结果 类似于这个游戏笔的东西: 使用std::thread::thread; #[导出(调试)] 结构Foo{ fn new(v:&'a u32)->Foo其中您编写的代码现在可以工作了: use std::thread; #[derive(Debug)] struct Foo<'a> { fp: &'a u32 } impl<'a> Foo<

这是我多次遇到的问题

  • 您有需要转到任务的数据

  • 您希望安全地将数据发送到任务并远程处理它

  • …而你需要等待结果

类似于这个游戏笔的东西:

使用std::thread::thread;
#[导出(调试)]
结构Foo{

fn new(v:&'a u32)->Foo其中您编写的代码现在可以工作了:

use std::thread;

#[derive(Debug)]
struct Foo<'a> {
    fp: &'a u32
}

impl<'a> Foo<'a> {
    fn new(v: &'a u32) -> Foo<'a> {
        Foo { fp: v }
    }
}

fn main() {
    let value = 100;
    let foo = Foo::new(&value);
    let guard = thread::scoped(|| {
        println!("{:?}", foo);
    });

    // We know foo is valid in the remote thread, because guard is in the same 
    // scope of foo... but how do we express that using lifetimes?
    guard.join();
}
使用std::thread;
#[导出(调试)]
结构Foo{

fn new(v:&'a u32)->FooThis PR可能与您试图实现的目标有关:仅供参考,Rust样式是4个空格的缩进。@Shepmaster拜托;我也不使用隐式返回语句;出于同样的原因;我不同意“标准”样式。好与否……我想我们都同意这与问题完全无关。
use std::thread;

#[derive(Debug)]
struct Foo<'a> {
    fp: &'a u32
}

impl<'a> Foo<'a> {
    fn new(v: &'a u32) -> Foo<'a> {
        Foo { fp: v }
    }
}

fn main() {
    let value = 100;
    let foo = Foo::new(&value);
    let guard = thread::scoped(|| {
        println!("{:?}", foo);
    });

    // We know foo is valid in the remote thread, because guard is in the same 
    // scope of foo... but how do we express that using lifetimes?
    guard.join();
}