Practice 07

String and str

Read each program and pick an answer. Some programs do not compile, and that can be the right answer. Your choice is checked as soon as you pick it, and saved in this browser.Need a refresher? Read lesson 7.

Course

0 of 50 answered·0 correct

Two Kinds of Text

1.

What does this print?

fn main() {
    let word = "rust";
    let owned = String::from("rust");
    println!("{}", word == owned);
    println!("{}", word.len());
    println!("{}", owned.len());
}
2.

Does this compile?

fn greeting() -> &str {
    "hi"
}
fn main() {
    println!("{}", greeting());
}
3.

What does this print?

fn greeting() -> &'static str {
    "hi"
}
fn main() {
    println!("{}", greeting());
}
4.

What does this print?

fn main() {
    let word = "rust";
    let upper = word.to_uppercase();
    println!("{word}");
    println!("{upper}");
}
5.

What does this print?

fn main() {
    let a: &str = "hello";
    let b: String = a.to_string();
    println!("{}", a == b);
    println!("{}", b == "hello");
}

Making a String

6.

What does this print?

fn main() {
    let a = String::from("cat");
    let b = "cat".to_string();
    let mut c = String::new();
    c.push_str("ca");
    c.push('t');
    println!("{}", a == b);
    println!("{}", b == c);
}
7.

What does this print?

fn main() {
    let mut s = String::new();
    s.push_str("Rust ");
    s.push_str("is ");
    s.push_str("fun");
    println!("{s}");
    println!("{}", s.len());
}
8.

What does this print?

fn main() {
    let name = String::from("Asha");
    let age = 9;
    let line = format!("{name} is {age}");
    println!("{line}");
    println!("{name}");
}
9.

Does this compile?

fn main() {
    let a = String::from("go");
    let b = String::from("od");
    let c = a + &b;
    println!("{c}");
    println!("{a}");
}
10.

What does this print?

fn main() {
    let a = String::from("a");
    let b = String::from("b");
    let c = String::from("c");
    let joined = a + &b + &c;
    println!("{joined}");
}
11.

What does this print?

fn main() {
    let mut s = String::from("hi");
    println!("{}", s.len());
    s.push('!');
    println!("{}", s.len());
}
12.

What does this print?

fn main() {
    let s = String::new();
    println!("{}", s.len());
    println!("{}", s.is_empty());
}

Text as a Function Parameter

13.

What does this print?

fn describe(text: &str) -> usize {
    text.len()
}

fn main() {
    let owned = String::from("hello");
    println!("{}", describe("hi"));
    println!("{}", describe(&owned));
    println!("{owned}");
}
14.

Does this compile?

fn shout(text: String) {
    println!("{}", text.to_uppercase());
}

fn main() {
    shout("hi");
}
15.

Does this compile?

fn keep(text: String) -> usize {
    text.len()
}

fn main() {
    let s = String::from("hello");
    let n = keep(s);
    println!("{n}");
    println!("{s}");
}
16.

What does this print?

fn shout(text: &mut String) {
    text.push('!');
}

fn main() {
    let mut s = String::from("hi");
    shout(&mut s);
    println!("{s}");
}
17.

What does this print?

fn greeting(name: &str) -> String {
    format!("Hello, {name}!")
}

fn main() {
    let name = String::from("Ravi");
    println!("{}", greeting(&name));
    println!("{}", greeting("Sam"));
    println!("{name}");
}

Length in Bytes and in Characters

18.

What does this print?

fn main() {
    let word = "cargo";
    println!("{}", word.len());
    println!("{}", word.chars().count());
}
19.

What does this print?

fn main() {
    let word = "café";
    println!("{}", word.len());
    println!("{}", word.chars().count());
}
20.

What does this print?

fn main() {
    let word = "naïve";
    println!("{}", word.len());
    println!("{}", word.chars().count());
}
21.

What does this print?

fn main() {
    let s = String::new();
    let t = String::from(" ");
    println!("{}", s.is_empty());
    println!("{}", t.is_empty());
    println!("{}", t.len());
}
22.

What does this print?

fn main() {
    let word = "año";
    println!("{}", word.len());
    println!("{}", word.chars().count());
}
23.

What does this print?

fn main() {
    let phrase = "café au lait";
    println!("{}", phrase.len());
    println!("{}", phrase.chars().count());
    println!("{}", phrase.split_whitespace().count());
}

Slices of Text

24.

What does this print?

fn main() {
    let text = String::from("rust lang");
    let first = &text[0..4];
    let second = &text[5..];
    println!("{first}");
    println!("{second}");
}
25.

What does this print?

fn main() {
    let text = "programming";
    let part = &text[0..7];
    println!("{part}");
}
26.

What does this print?

fn main() {
    let text = String::from("hi there");
    let all = &text[..];
    println!("{all}");
    println!("{}", all.len());
}
27.

Does this compile? What happens when you run it?

fn main() {
    let text = String::from("café");
    let cut = &text[0..4];
    println!("{cut}");
}
28.

Does this compile?

fn main() {
    let text = String::from("hello");
    let first = text[0];
    println!("{first}");
}
29.

What does this print?

fn main() {
    let text = String::from("rustacean");
    let start = &text[0..4];
    println!("{}", start == "rust");
}
30.

What does this print?

fn main() {
    let mut text = String::from("cat");
    let borrowed = &text[0..3];
    println!("{borrowed}");
    text.push_str("erpillar");
    println!("{text}");
}

Stepping Through the Characters

31.

What does this print?

fn main() {
    let word = "abc";
    for c in word.chars() {
        println!("{c}");
    }
}
32.

What does this print?

fn main() {
    let word = "noël";
    for (i, c) in word.chars().enumerate() {
        println!("{i}: {c}");
    }
}
33.

What does this print?

fn main() {
    let text = "banana";
    let mut count = 0;
    for c in text.chars() {
        if c == 'a' {
            count += 1;
        }
    }
    println!("{count}");
}
34.

What does this print?

fn main() {
    let mut result = String::new();
    for c in "abc".chars() {
        result.insert(0, c);
    }
    println!("{result}");
}
35.

What does this print?

fn main() {
    let letters = "xyz";
    for c in letters.chars() {
        println!("{}", "aeiou".contains(c));
    }
}

Comparing Text

36.

What does this print?

fn main() {
    let a = String::from("Rust");
    let b = "rust";
    println!("{}", a == b);
    println!("{}", a.to_lowercase() == b);
}
37.

What does this print?

fn main() {
    let a = String::from("cat");
    let b = String::from("cat");
    println!("{}", a == b);
}
38.

What does this print?

fn main() {
    let a = String::from("go ");
    println!("{}", a == "go");
    println!("{}", a.trim() == "go");
}

Common Methods

39.

What does this print?

fn main() {
    let s = String::from("  Learn Rust  ");
    println!("[{}]", s.trim());
    println!("{}", s.contains("Rust"));
}
40.

What does this print?

fn main() {
    let s = "hello world";
    println!("{}", s.starts_with("hello"));
    println!("{}", s.ends_with("world"));
    println!("{}", s.ends_with("World"));
}
41.

What does this print?

fn main() {
    let s = String::from("one two three");
    println!("{}", s.split_whitespace().count());
    println!("{}", s.replace("two", "2"));
}
42.

What does this print?

fn main() {
    let s = "RUST";
    println!("{}", s.to_lowercase());
    println!("{s}");
}
43.

What does this print?

fn main() {
    let s = String::from("   ");
    println!("{}", s.is_empty());
    println!("{}", s.trim().is_empty());
}

Text and Numbers

44.

What does this print?

fn main() {
    let text = "7";
    let n: i32 = text.parse().expect("not a number");
    println!("{}", n * 3);
}
45.

What does this print when you run it?

fn main() {
    let text = "seven";
    let n: i32 = text.parse().expect("not a number");
    println!("{n}");
}
46.

What does this print?

fn main() {
    let n = 15;
    let text = n.to_string();
    println!("{}", text.len());
    println!("{}", text + " points");
}
47.

What does this print?

fn main() {
    let price: f64 = "19.99".parse().expect("not a number");
    println!("{}", price + 0.01);
}

Reading from the Keyboard

48.

The user types blue. What does the last line say?

use std::io;

fn main() {
    println!("Type a color:");
    let mut color = String::new();
    io::stdin().read_line(&mut color).expect("could not read the line");
    println!("You chose {}", color.trim());
}

The terminal so far

Type a color:
blue
49.

The user types 8. What does the last line say?

use std::io;

fn main() {
    println!("Type a whole number:");
    let mut line = String::new();
    io::stdin().read_line(&mut line).expect("could not read the line");
    let n: i32 = line.trim().parse().expect("not a whole number");
    println!("double: {}", n * 2);
}

The terminal so far

Type a whole number:
8
50.

The trim() was left out before parse(). The user types 8. What happens?

use std::io;

fn main() {
    println!("Type a whole number:");
    let mut line = String::new();
    io::stdin().read_line(&mut line).expect("could not read the line");
    let n: i32 = line.parse().expect("not a whole number");
    println!("double: {}", n * 2);
}

The terminal so far

Type a whole number:
8