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

后端 未结 2 1573
有刺的猬
有刺的猬 2020-11-29 13:11

Given the following function:

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

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


        
2条回答
  •  伪装坚强ぢ
    2020-11-29 13:32

    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:

    • 37407
    • 21114

提交回复
热议问题