You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fn main() {
let foo: String = std::io::stdin().lock().lines().nth(0).unwrap().unwrap();
}
leads to
src\main.rs:14:23: 14:39 error: borrowed value does not live long enough
src\main.rs:14 let foo: String = std::io::stdin().lock().lines().nth(1).unwrap().unwrap();
^~~~~~~~~~~~~~~~
src\main.rs:14:5: 14:80 note: reference must be valid for the destruction scope surrounding statement at 14:4...
src\main.rs:14 let foo: String = std::io::stdin().lock().lines().nth(1).unwrap().unwrap();
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src\main.rs:14:5: 14:80 note: ...but borrowed value is only valid for the statement at 14:4
src\main.rs:14 let foo: String = std::io::stdin().lock().lines().nth(1).unwrap().unwrap();
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src\main.rs:14:5: 14:80 help: consider using a `let` binding to increase its lifetime
src\main.rs:14 let foo: String = std::io::stdin().lock().lines().nth(1).unwrap().unwrap();
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This doesn't make any sense since foo does not reference any temporary object (like Stdin or StdinLock). It's a String after all, as the optional type annotation shows.
The problem here is that std::io::stdin() creates a Stdin, whose lock method needs to borrow itself. However, the lifetime of the Stdin is apparently too short if it isn't bound to anything. You can fix it like so (and maybe await a better explanation):
use std::io::BufRead;fnmain(){let bar = std::io::stdin();let foo:String = bar.lock().lines().nth(0).unwrap().unwrap();}
Yep, that's the fix I currently use. Sorry, should have been more clear about that.
Still: I would expect that rustc automagically creates bindings here, just as in let bar = &3;. I don't know if there is an issue about this already :/
This is an issue that will be fixed by "non-lexical lifetimes", so I will give it a close, as it falls under that umbrella. The RFC is not yet up, but it is a very desired thing. rust-lang/rfcs#811 is one of the two or three RFC issues on this topic.
leads to
This doesn't make any sense since
foo
does not reference any temporary object (likeStdin
orStdinLock
). It's aString
after all, as the optional type annotation shows.Maybe related to #31439
The text was updated successfully, but these errors were encountered: