Menu
Coddy logo textTech

Reading User Input

Part of the Fundamentals section of Coddy's Rust journey — lesson 30 of 75.

So far, we've been creating programs where we directly write values in our code. But real programs often need to interact with users and get information from them.

In Rust, getting input from a user is done using the std::io library. This library provides methods to read different types of input, such as integers, floating-point numbers, and strings.

To use the std::io library, you first need to import it:

use std::io;

To read input from a user, we need two lines of code:

let mut my_var = String::new();
// Creates an empty string to store the input

io::stdin().read_line(&mut my_var).unwrap();
 // Reads the input

This will store the input value in the my_var variable as a string type.

If you want to get a number, you need an extra step to convert the text input into a number:

let age: i32 = input.trim().parse().unwrap();

The .trim() is needed because when users press Enter, a newline character is added to their input. For example, if someone types "25" and presses Enter, the actual input is "25\n". The .trim() removes these extra whitespace characters (spaces, newlines, etc.) from both the start and end of the input.

Don't worry too much about unwrap() for now - we'll learn about it later. Just know that it helps us handle potential errors in a simple (though not ideal) way.

challenge icon

Challenge

Beginner

Write a program that gets input from the user (their name), and then outputs Hello, followed by the user's inputted name and an exclamation mark.

For example, if the user inputs Bob, the expected output is Hello, Bob!.

You will need to:

  1. Create a String object to read input.
  2. Prompt the user to enter their name: "Enter your name:"
  3. Read the user's name using the appropriate std::io method.
  4. Print Hello, followed by the stored name and an exclamation mark ! at the end.

Cheat sheet

To get user input in Rust, import the std::io library:

use std::io;

To read string input from a user:

let mut my_var = String::new();
io::stdin().read_line(&mut my_var).unwrap();

To convert string input to a number:

let age: i32 = input.trim().parse().unwrap();

The .trim() method removes whitespace characters (including newlines from pressing Enter) from the input.

Try it yourself

use std::io;

fn main() {
    // Create a String object
    let mut name = String::new();
    
    // Prompt the user to enter their name (use println)
    
    
    // Read the user's name
    
    
    // Print the greeting message
    
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals