I am learning Rust and I wrote the following code to read an array of integers from stdin :
use std::io; fn main() { for line in io::stdin().lines() { let xs:Vec<int> = line.unwrap().as_slice().trim().split(' ') .map(|s|from_str::<int>(s).unwrap()).collect(); println!("{}", xs); } }
This worked fine, however, I felt the let xs line was a bit long, so I split it into two parts:
fn main() { for line in io::stdin().lines() { let ss = line.unwrap().as_slice().trim().split(' '); let xs:Vec<int> = ss.map(|s|from_str::<int>(s).unwrap()).collect(); println!("{}", xs); } }
It didn’t work! Rust responded with the following error:
hello.rs:4:12: 4:25 error: borrowed value does not live long enough hello.rs:4 let ss = line.unwrap().as_slice().trim().split(' '); ^~~~~~~~~~~~~ hello.rs:3:34: 7:3 note: reference must be valid for the block at 3:33... hello.rs:3 for line in io::stdin().lines() { hello.rs:4 let ss = line.unwrap().as_slice().trim().split(' '); hello.rs:5 let xs:Vec<int> = ss.map(|s|from_str::<int>(s).unwrap()).collect(); hello.rs:6 println!("{}", xs); hello.rs:7 } hello.rs:4:3: 4:54 note: ...but borrowed value is only valid for the statement at 4:2; consider using a `let` binding to increase its lifetime hello.rs:4 let ss = line.unwrap().as_slice().trim().split(' '); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error: aborting due to previous error
It bothers me. Is it a line or ss that doesn't live long enough? And how can I use let binding to increase their lifespan? I thought I was already using let?
I searched the Internet for solutions and read the Lifetime guide , but I still can't figure it out. Can someone give me a hint?
let rust lifetime
Thomas ahle
source share