What does this print?
fn main() {
let word = "rust";
let owned = String::from("rust");
println!("{}", word == owned);
println!("{}", word.len());
println!("{}", owned.len());
}Running it prints:
true 4 4
Practice 07
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.
0 of 50 answered·0 correct
What does this print?
fn main() {
let word = "rust";
let owned = String::from("rust");
println!("{}", word == owned);
println!("{}", word.len());
println!("{}", owned.len());
}Running it prints:
true 4 4
Does this compile?
fn greeting() -> &str {
"hi"
}
fn main() {
println!("{}", greeting());
}The compiler says:
error[E0106]: missing lifetime specifier
What does this print?
fn greeting() -> &'static str {
"hi"
}
fn main() {
println!("{}", greeting());
}Running it prints:
hi
What does this print?
fn main() {
let word = "rust";
let upper = word.to_uppercase();
println!("{word}");
println!("{upper}");
}Running it prints:
rust RUST
What does this print?
fn main() {
let a: &str = "hello";
let b: String = a.to_string();
println!("{}", a == b);
println!("{}", b == "hello");
}Running it prints:
true true
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);
}Running it prints:
true true
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());
}Running it prints:
Rust is fun 11
What does this print?
fn main() {
let name = String::from("Asha");
let age = 9;
let line = format!("{name} is {age}");
println!("{line}");
println!("{name}");
}Running it prints:
Asha is 9 Asha
Does this compile?
fn main() {
let a = String::from("go");
let b = String::from("od");
let c = a + &b;
println!("{c}");
println!("{a}");
}The compiler says:
error[E0382]: borrow of moved value: `a`
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}");
}Running it prints:
abc
What does this print?
fn main() {
let mut s = String::from("hi");
println!("{}", s.len());
s.push('!');
println!("{}", s.len());
}Running it prints:
2 3
What does this print?
fn main() {
let s = String::new();
println!("{}", s.len());
println!("{}", s.is_empty());
}Running it prints:
0 true
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}");
}Running it prints:
2 5 hello
Does this compile?
fn shout(text: String) {
println!("{}", text.to_uppercase());
}
fn main() {
shout("hi");
}The compiler says:
error[E0308]: mismatched types
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}");
}The compiler says:
error[E0382]: borrow of moved value: `s`
What does this print?
fn shout(text: &mut String) {
text.push('!');
}
fn main() {
let mut s = String::from("hi");
shout(&mut s);
println!("{s}");
}Running it prints:
hi!
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}");
}Running it prints:
Hello, Ravi! Hello, Sam! Ravi
What does this print?
fn main() {
let word = "cargo";
println!("{}", word.len());
println!("{}", word.chars().count());
}Running it prints:
5 5
What does this print?
fn main() {
let word = "café";
println!("{}", word.len());
println!("{}", word.chars().count());
}Running it prints:
5 4
What does this print?
fn main() {
let word = "naïve";
println!("{}", word.len());
println!("{}", word.chars().count());
}Running it prints:
6 5
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());
}Running it prints:
true false 1
What does this print?
fn main() {
let word = "año";
println!("{}", word.len());
println!("{}", word.chars().count());
}Running it prints:
4 3
What does this print?
fn main() {
let phrase = "café au lait";
println!("{}", phrase.len());
println!("{}", phrase.chars().count());
println!("{}", phrase.split_whitespace().count());
}Running it prints:
13 12 3
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}");
}Running it prints:
rust lang
What does this print?
fn main() {
let text = "programming";
let part = &text[0..7];
println!("{part}");
}Running it prints:
program
What does this print?
fn main() {
let text = String::from("hi there");
let all = &text[..];
println!("{all}");
println!("{}", all.len());
}Running it prints:
hi there 8
Does this compile? What happens when you run it?
fn main() {
let text = String::from("café");
let cut = &text[0..4];
println!("{cut}");
}Running it prints nothing.
Then it panics: end byte index 4 is not a char boundary; it is inside 'é' (bytes 3..5 of string)
Does this compile?
fn main() {
let text = String::from("hello");
let first = text[0];
println!("{first}");
}The compiler says:
error[E0277]: the type `str` cannot be indexed by `{integer}`What does this print?
fn main() {
let text = String::from("rustacean");
let start = &text[0..4];
println!("{}", start == "rust");
}Running it prints:
true
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}");
}Running it prints:
cat caterpillar
What does this print?
fn main() {
let word = "abc";
for c in word.chars() {
println!("{c}");
}
}Running it prints:
a b c
What does this print?
fn main() {
let word = "noël";
for (i, c) in word.chars().enumerate() {
println!("{i}: {c}");
}
}Running it prints:
0: n 1: o 2: ë 3: l
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}");
}Running it prints:
3
What does this print?
fn main() {
let mut result = String::new();
for c in "abc".chars() {
result.insert(0, c);
}
println!("{result}");
}Running it prints:
cba
What does this print?
fn main() {
let letters = "xyz";
for c in letters.chars() {
println!("{}", "aeiou".contains(c));
}
}Running it prints:
false false false
What does this print?
fn main() {
let a = String::from("Rust");
let b = "rust";
println!("{}", a == b);
println!("{}", a.to_lowercase() == b);
}Running it prints:
false true
What does this print?
fn main() {
let a = String::from("cat");
let b = String::from("cat");
println!("{}", a == b);
}Running it prints:
true
What does this print?
fn main() {
let a = String::from("go ");
println!("{}", a == "go");
println!("{}", a.trim() == "go");
}Running it prints:
false true
What does this print?
fn main() {
let s = String::from(" Learn Rust ");
println!("[{}]", s.trim());
println!("{}", s.contains("Rust"));
}Running it prints:
[Learn Rust] true
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"));
}Running it prints:
true true false
What does this print?
fn main() {
let s = String::from("one two three");
println!("{}", s.split_whitespace().count());
println!("{}", s.replace("two", "2"));
}Running it prints:
3 one 2 three
What does this print?
fn main() {
let s = "RUST";
println!("{}", s.to_lowercase());
println!("{s}");
}Running it prints:
rust RUST
What does this print?
fn main() {
let s = String::from(" ");
println!("{}", s.is_empty());
println!("{}", s.trim().is_empty());
}Running it prints:
false true
What does this print?
fn main() {
let text = "7";
let n: i32 = text.parse().expect("not a number");
println!("{}", n * 3);
}Running it prints:
21
What does this print when you run it?
fn main() {
let text = "seven";
let n: i32 = text.parse().expect("not a number");
println!("{n}");
}Running it prints nothing.
Then it panics: not a number: ParseIntError { kind: InvalidDigit }
What does this print?
fn main() {
let n = 15;
let text = n.to_string();
println!("{}", text.len());
println!("{}", text + " points");
}Running it prints:
2 15 points
What does this print?
fn main() {
let price: f64 = "19.99".parse().expect("not a number");
println!("{}", price + 0.01);
}Running it prints:
20
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
Running it prints:
Type a color: You chose blue
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
Running it prints:
Type a whole number: double: 16
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
Running it prints:
Type a whole number:
Then it panics: not a whole number: ParseIntError { kind: InvalidDigit }