0
0
Rustprogramming~30 mins

Reading input basics in Rust - Mini Project: Build & Apply

Choose your learning style9 modes available
Reading input basics
๐Ÿ“– Scenario: You are creating a simple Rust program that asks the user for their name and age. This is a common task when you want to interact with people using your program.
๐ŸŽฏ Goal: Build a Rust program that reads the user's name and age from the keyboard and stores them in variables.
๐Ÿ“‹ What You'll Learn
Use the std::io module to read input
Create variables to store the user's name and age
Trim the input to remove extra spaces or newlines
Convert the age input from a string to a number
๐Ÿ’ก Why This Matters
๐ŸŒ Real World
Reading input from users is essential for interactive programs like games, quizzes, or command-line tools.
๐Ÿ’ผ Career
Many programming jobs require handling user input safely and correctly, especially in backend or systems programming.
Progress0 / 4 steps
1
Create variables to store user input
Write code to create two mutable variables called name and age as empty String values.
Rust
Need a hint?

Use String::new() to create empty strings for name and age.

2
Read user input for name and age
Add code to read input from the user into the variables name and age using io::stdin().read_line(&mut name) and io::stdin().read_line(&mut age).
Rust
Need a hint?

Use io::stdin().read_line(&mut variable) to read input into each variable.

3
Trim input and convert age to number
Add code to trim whitespace from name and age. Then convert age from a string to an u32 number using age.trim().parse::() and store it in a new variable called age_num.
Rust
Need a hint?

Use trim() to remove spaces and parse() to convert the age string to a number.

4
Print the user's name and age
Write a println! statement to display the message: Hello, {name}! You are {age_num} years old. using the variables name and age_num.
Rust
Need a hint?

Use println!("Hello, {}! You are {} years old.", name, age_num); to show the message.