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

后端 未结 2 1571
有刺的猬
有刺的猬 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:30

    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)

提交回复
热议问题