read line rust
To read a line in Rust, you can use the std::io::stdin
module to read input from the user. Here's an example code snippet:
use std::io;
fn main() {
let mut input = String::new();
match io::stdin().read_line(&mut input) {
Ok(_) => {
println!("You entered: {}", input);
}
Err(error) => {
println!("Error reading input: {}", error);
}
}
}
In this code, we first create a mutable input
variable of type String
to store the user's input. Then, we use io::stdin().read_line(&mut input)
to read a line from the standard input and store it in the input
variable. Finally, we print the input using println!("You entered: {}", input)
.
Please note that this code snippet assumes that you have already imported the std::io
module. If you haven't, you can add use std::io;
at the beginning of your Rust file.
I hope this helps! Let me know if you have any further questions.