Rustlang is all about its functions. You barely have to run your program to make sure it works, and when you do, you write unit tests for them. But, what about good old days of taking keyboard input and faffing about without end in your program? Here's how to bring that back in Rust.
The Program
This program will take input and parrot it back to you. It's as simple as simple gets.
use std::io;
fn main(){
print!("Input: ");
println!("{}", io::stdin().read_line().unwrap());
}
io::stdin() returns a BufferedReader on stdio (the input stream). read_line() returns an IoResult (just used for handling some errors) that .unwrap() turns into a normal string. That is then printed by the println! macro.
Just be careful with .unwrap(). Part of Rust's power is being able to match against errors, and you lose that with .unwrap().
No comments:
Post a Comment