Last active
August 29, 2015 14:00
-
-
Save mnylen/399f093915aefc26dc16 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io::BufferedReader; | |
use std::io; | |
fn main() { | |
let mut reader = BufferedReader::new(io::stdin()); | |
println!("What is your name?"); | |
let age_input: ~str = reader.read_line().unwrap(); | |
let age_trimmed: &str = age_input.trim(); | |
println!("Hello, {}!", age_trimmed); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io::BufferedReader; | |
use std::io; | |
fn main() { | |
let mut reader = BufferedReader::new(io::stdin()); | |
println!("What is your name?"); | |
let age_input: &str = reader.read_line().unwrap().trim(); | |
println!("Hello, {}!", age_input); | |
} |
let age_input = reader.read_line().unwrap().trim
is roughly equivalent to:
let age_input = {
let tmp1 = reader.read_line();
let tmp2 = tmp1.unwrap();
tmp2.trim()
}
trim
's signature istrim(&self) -> &str
. Callingtmp2.trim()
thus borrowstmp2
.- the return value of
trim
is a reference to a substring oftmp2
, which still refers the originaltmp2
. - at the end of
{ ... }
block, thetmp2
- the original input string - gets destroyed - so, now that we don't have
tmp2
, the reference to substring to it is no longer valid and rust compiler detects this
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Getting this error when compiling
hello_error.rs
: