Complete the code to read a line of input from the user.
use std::io;
fn main() {
let mut input = String::new();
io::stdin().[1](&mut input).expect("Failed to read line");
println!("You typed: {}", input);
}The read_line method reads a line from standard input into the given string buffer.
Complete the code to trim whitespace from the input string.
use std::io;
fn main() {
let mut input = String::new();
io::stdin().read_line(&mut input).expect("Failed to read line");
let trimmed = input.[1]();
println!("Trimmed input: {}", trimmed);
}The trim method removes whitespace from both ends of a string.
Fix the error in the code to parse the input string into an integer.
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.[1]().parse().expect("Not a number");
println!("Number is: {}", num);
}We use trim to remove newline and spaces before parsing the string to an integer.
Fill both blanks to create a dictionary (HashMap) from words to their lengths, filtering words longer than 3 characters.
use std::collections::HashMap;
fn main() {
let words = vec!["apple", "cat", "banana", "dog"];
let lengths: HashMap<_, _> = words.iter().map(|word| (word, word.len())).filter(|(_, &len)| len [1] 3).collect();
println!("{:?}", lengths);
}The filter keeps words whose length is greater than 3.
Fill all three blanks to read input, trim it, parse to i32, and print the number.
use std::io;
fn main() {
let mut input = String::new();
io::stdin().[1](&mut input).expect("Failed to read line");
let num: i32 = input.[2]().parse().expect("Not a number");
println!("You entered: {}", [3]);
}First, read a line with read_line. Then trim whitespace with trim. Finally, print the parsed number variable num.