When returning the outcome of consuming a StdinLock, why was the borrow to stdin retained?

爷,独闯天下 提交于 2019-11-27 02:14:14
musicmatze

This seems to be a bug in the compiler. You can make the compiler happy by using an explicit return statement:

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

fn foo() -> usize {
    let stdin = stdin();
    let stdinlock = stdin.lock();
    return stdinlock
        .lines()
        .count();
}

fn main() {}

playground

As mentioned in the comments, there are multiple Rust issues related to this:

I cannot answer the why of your question, but I can state that the current1 implementation of non-lexical lifetimes allows the original code to compile:

#![feature(nll)]

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

fn foo() -> usize {
    let stdin = stdin();
    let stdinlock = stdin.lock();
    stdinlock
        .lines()
        .count()
}

Playground

1 1.25.0-nightly (2018-01-11 73ac5d6)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!