User input
Here is an example of how to take user input in a Rust program:
use std::io; fn main() { let mut input = String::new(); io::stdin().read_line(&mut input).expect("Failed to read line"); println!("The user input is: {}", input); }
In this example, the read_line()
function from the std::io
module is used to read a line of user input. The read_line()
function takes a mutable string as an argument, and it returns an io::Result
. The io::Result
enum represents the result of an I/O operation, and it can either be Ok
or Err
.
If the read_line()
function succeeds, it returns Ok(input)
, where input
is the string that was entered by the user. If the read_line()
function fails, it returns Err(error)
, where error
is an error message.
The expect()
macro is used to handle the io::Result
from the read_line()
function. The expect()
macro takes a Result
as an argument, and it panics if the Result
is Err
. In this example, the expect()
macro will panic if the read_line()
function fails.
The main()
function prints the user input to the console.
Here is an example of how to run the program:
Code snippet
cargo run
When you run the program, you will be prompted to enter some text. After you enter the text, the program will print the text that you entered to the console.
To capture user input in a Rust program, you can use the std::io
module to read input from the standard input stream (stdin
). Here's an example that demonstrates how to get user input in Rust:
use std::io; fn main() { println!("Please enter your name:"); let mut name = String::new(); io::stdin() .read_line(&mut name) .expect("Failed to read line"); println!("Hello, {}!", name.trim()); }
In this example, the program prompts the user to enter their name. It creates a mutable String
variable called name
to store the input. The read_line
function from std::io::stdin()
reads the input from the user and appends it to the name
variable. The expect
method is used to handle any errors that may occur during input reading.
Finally, the program trims the input using the trim
method to remove any leading or trailing whitespace. It then prints a greeting message along with the user's name.
You can run this program and interact with it by providing your name as input.