0
0
Rustprogramming~15 mins

Why input and output are required in Rust - See It in Action

Choose your learning style9 modes available
Why input and output are required
๐Ÿ“– Scenario: Imagine you want to create a simple program that talks with a user. The program needs to get information from the user and then show something back. This is why input and output are important.
๐ŸŽฏ Goal: Build a Rust program that asks the user for their name (input) and then greets them by name (output).
๐Ÿ“‹ What You'll Learn
Use std::io::stdin to get input from the user
Store the input in a variable called name
Print a greeting message using println! with the user's name
๐Ÿ’ก Why This Matters
๐ŸŒ Real World
Many programs need to talk with people. Input and output help programs get data and show results.
๐Ÿ’ผ Career
Understanding input and output is key for writing programs that users can interact with, a basic skill for any software developer.
Progress0 / 4 steps
1
Create a variable to store user input
Write a line to create a mutable String variable called name and set it to an empty string.
Rust
Need a hint?

Use let mut name = String::new(); to create an empty string that can change.

2
Read input from the user
Add a line to read a line from the user into the name variable using std::io::stdin().read_line(&mut name) and handle the result with .expect("Failed to read line").
Rust
Need a hint?

Use std::io::stdin().read_line(&mut name).expect("Failed to read line") to get input.

3
Remove newline from input
Add a line to remove the newline character from the name variable using name = name.trim().to_string().
Rust
Need a hint?

Use name = name.trim().to_string() to clean the input.

4
Print a greeting message
Write a line to print Hello, {name}! using println!("Hello, {}!", name).
Rust
Need a hint?

Use println!("Hello, {}!", name); to show the greeting.