使用`let`绑定来增加值的寿命 [英] Using a `let` binding to increase a values lifetime

查看:139
本文介绍了使用`let`绑定来增加值的寿命的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下代码,以从stdin读取整数数组:

I wrote the following code to read an array of integers from stdin:

use std::io::{self, BufRead};

fn main() {
    let stdin = io::stdin();
    for line in stdin.lock().lines() {
        let xs: Vec<i32> = line.unwrap()
            .trim()
            .split(' ')
            .map(|s| s.parse().unwrap())
            .collect();

        println!("{:?}", xs);
    }
}

这很好,但是,我觉得let xs行有点长,所以我将其分为两部分:

This worked fine, however, I felt the let xs line was a bit long, so I split it into two:

use std::io::{self, BufRead};

fn main() {
    let stdin = io::stdin();
    for line in stdin.lock().lines() {
        let ss = line.unwrap().trim().split(' ');
        let xs: Vec<i32> = ss.map(|s| s.parse().unwrap()).collect();

        println!("{:?}", xs);
    }
}

这没用! Rust回复了以下错误:

This didn't work! Rust replied with the following error:

error[E0597]: borrowed value does not live long enough
  --> src/main.rs:6:18
   |
6  |         let ss = line.unwrap().trim().split(' ');
   |                  ^^^^^^^^^^^^^                  - temporary value dropped here while still borrowed
   |                  |
   |                  temporary value does not live long enough
...
10 |     }
   |     - temporary value needs to live until here
   |
   = note: consider using a `let` binding to increase its lifetime

这使我感到困惑.是liness寿命不够长?以及如何使用let绑定来延长其寿命?我以为我已经在使用let?

This confuses me. Is it line or ss that doesn't live long enough? And how can I use a let binding to increase their lifetime? I thought I was already using a let?

我已阅读

I've read through the lifetime guide, but I still can't quite figure it out. Can anyone give me a hint?

推荐答案

在第二个版本中,ss的类型为 unwrap() 消耗line ;换句话说,它将Ok变体的数据移出Result对象.因此,该引用不是指向原始line内部,而是指向一个临时对象.

In your second version, the type of ss is Split<'a, char>. The lifetime parameter in the type tells us that the object contains a reference. In order for the assignment to be valid, the reference must point to an object that exists after that statement. However, unwrap() consumes line; in other words, it moves Ok variant's data out of the Result object. Therefore, the reference doesn't point inside the original line, but rather on a temporary object.

在第一个版本中,尽管调用了map,但在长表达式的末尾消耗了临时变量.要修复第二个版本,您需要绑定unwrap()的结果以使该值有效期足够长:

In your first version, you consume the temporary by the end of the long expression, though the call to map. To fix your second version, you need to bind the result of unwrap() to keep the value living long enough:

use std::io::{self, BufRead};

fn main() {
    let stdin = io::stdin();
    for line in stdin.lock().lines() {
        let line = line.unwrap();
        let ss = line.trim().split(' ');
        let xs: Vec<i32> = ss.map(|s| s.parse().unwrap()).collect();

        println!("{:?}", xs);
    }
}

这篇关于使用`let`绑定来增加值的寿命的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆