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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
let age_input = reader.read_line().unwrap().trim
is roughly equivalent to:trim
's signature istrim(&self) -> &str
. Callingtmp2.trim()
thus borrowstmp2
.trim
is a reference to a substring oftmp2
, which still refers the originaltmp2
.{ ... }
block, thetmp2
- the original input string - gets destroyedtmp2
, the reference to substring to it is no longer valid and rust compiler detects this