0
0
Rustprogramming~10 mins

Handling input values in Rust - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to read a line of input from the user.

Rust
use std::io;

fn main() {
    let mut input = String::new();
    io::stdin().[1](&mut input).expect("Failed to read line");
    println!("You typed: {}", input.trim());
}
Drag options to blanks, or click blank then click option'
Aread_exact
Bread_to_string
Cread_line
Dread
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using read_to_string which reads until EOF, not just one line.
Using read_exact which requires a fixed-size buffer.
2fill in blank
medium

Complete the code to convert the input string to an integer.

Rust
use std::io;

fn main() {
    let mut input = String::new();
    io::stdin().read_line(&mut input).expect("Failed to read line");
    let num: i32 = input.trim().[1]().expect("Please type a number!");
    println!("You typed the number: {}", num);
}
Drag options to blanks, or click blank then click option'
Aparse
Bto_string
Cparse_int
Dto_int
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using non-existent methods like to_int or parse_int.
Forgetting to trim the input before parsing.
3fill in blank
hard

Fix the error in the code to correctly handle input and parse it as a floating-point number.

Rust
use std::io;

fn main() {
    let mut input = String::new();
    io::stdin().read_line(&mut input).expect("Failed to read line");
    let num: f64 = input.[1]().expect("Please type a number!");
    println!("You typed the number: {}", num);
}
Drag options to blanks, or click blank then click option'
Aparse
Btrim
Cto_string
Dparse_float
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Calling parse directly on the input string with newline characters.
Using non-existent methods like parse_float.
4fill in blank
hard

Fill both blanks to read input, trim it, and parse it as an unsigned integer.

Rust
use std::io;

fn main() {
    let mut input = String::new();
    io::stdin().[1](&mut input).expect("Failed to read line");
    let num: u32 = input.[2]().parse().expect("Please type a number!");
    println!("Number: {}", num);
}
Drag options to blanks, or click blank then click option'
Aread_line
Bread_to_string
Ctrim
Dto_string
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using read_to_string which reads until EOF.
Not trimming input before parsing.
5fill in blank
hard

Fill all three blanks to read input, trim it, parse it as i64, and handle possible errors.

Rust
use std::io;

fn main() {
    let mut input = String::new();
    io::stdin().[1](&mut input).expect("Failed to read line");
    let trimmed = input.[2]();
    let num: i64 = match trimmed.parse() {
        Ok(n) => n,
        Err(_) => {
            println!("Invalid number");
            return;
        }
    };
    println!("You typed: {}", num);
}
Drag options to blanks, or click blank then click option'
Aread_line
Btrim
Cparse
Dto_string
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Not handling parse errors with match.
Using to_string unnecessarily.