What does this print?
fn main() {
let a = String::from("hi");
let b = a;
println!("{b}");
}Running it prints:
hi
Practice 06
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 6.
0 of 50 answered·0 correct
What does this print?
fn main() {
let a = String::from("hi");
let b = a;
println!("{b}");
}Running it prints:
hi
What does this print?
fn main() {
let a = String::from("hi");
let b = a;
println!("{a}");
}The compiler says:
error[E0382]: borrow of moved value: `a`
Why: A move is not a copy. a stops working the moment b takes the value, so the last line has nothing to print.
What does this print?
fn main() {
let a = 5;
let b = a;
println!("{a} {b}");
}Running it prints:
5 5
What does this print?
fn main() {
let a = (1, 2);
let b = a;
println!("{} {}", a.0, b.1);
}Running it prints:
1 2
What does this print?
fn main() {
let a = (1, String::from("x"));
let b = a;
println!("{}", a.0);
}The compiler says:
error[E0382]: borrow of moved value: `a`
Why: The whole tuple moves, not just the String inside it. a.0 is a number, but it went with the rest of the tuple, so you cannot read it either.
What does this print?
fn main() {
let flag = true;
let copy = flag;
println!("{flag} {copy}");
}Running it prints:
true true
What does this print?
fn main() {
let letter = 'R';
let copy = letter;
println!("{letter}{copy}");
}Running it prints:
RR
What does this print?
fn main() {
let count = 3;
let count = count + 1;
println!("{count}");
}Running it prints:
4
Why: The second let does not change the first variable. It creates a new variable with the same name, and the old one is hidden from there on.
What does this print?
fn main() {
let a = String::from("hi");
let b = a.clone();
println!("{a} {b}");
}Running it prints:
hi hi
What does this print?
fn main() {
let text = String::from("abc");
println!("{}", text.len());
}Running it prints:
3
What does this print?
fn main() {
let text = String::from("a b c");
println!("{}", text.len());
}Running it prints:
5
What does this print?
fn main() {
let mut text = String::from("ab");
text.push_str("cd");
println!("{} {}", text, text.len());
}Running it prints:
abcd 4
What does this print?
fn main() {
let a = String::from("hi");
let b = a.clone();
println!("{}", a.len() + b.len());
}Running it prints:
4
Why: Both strings are two characters long, and len returns a number, so the line prints the sum and not the text.
What does this print?
fn main() {
let mut text = String::from("one");
text = String::from("two");
println!("{text}");
}Running it prints:
two
Why: Assigning to text drops the first String and puts the second one in its place. Nothing is left of one.
What does this print?
fn main() {
let a = String::from("hi");
let b = a.clone();
let c = b;
println!("{a} {c}");
}Running it prints:
hi hi
Why: b is a real copy, so a still works. Then c takes the value from b, so it is b that stops working, not a.
What does this print?
fn take(text: String) -> usize {
text.len()
}
fn main() {
let name = String::from("ferris");
println!("{}", take(name.clone()));
println!("{name}");
}Running it prints:
6 ferris
Why: The clone is the value that moves into take. name was never given away, so the line after it still prints.
What does this print?
fn main() {
{
let inside = String::from("hello");
println!("{inside}");
}
println!("done");
}Running it prints:
hello done
What does this print?
fn main() {
{
let inside = String::from("hello");
}
println!("{inside}");
}The compiler says:
error[E0425]: cannot find value `inside` in this scope
Why: This is not a move error. The variable ended at the closing brace of the block, so the name does not exist on the last line at all.
What does this print?
fn main() {
let outer = String::from("out");
{
let inner = &outer;
println!("{inner}");
}
println!("{outer}");
}Running it prints:
out out
What does this print?
fn main() {
let text = String::from("hi");
{
let moved = text;
println!("{moved}");
}
println!("{text}");
}The compiler says:
error[E0382]: borrow of moved value: `text`
Why: The value moved into moved inside the block, and moved dropped it at the closing brace. The outer variable is dead from the assignment on, even though it was written outside the block.
What does this print?
fn show(text: String) {
println!("{text}");
}
fn main() {
let name = String::from("ferris");
show(name);
println!("{name}");
}The compiler says:
error[E0382]: borrow of moved value: `name`
What does this print?
fn length(text: String) -> String {
println!("{}", text.len());
text
}
fn main() {
let name = String::from("ferris");
let name = length(name);
println!("{name}");
}Running it prints:
6 ferris
What does this print?
fn double(n: i32) -> i32 {
n * 2
}
fn main() {
let count = 4;
println!("{}", double(count) + count);
}Running it prints:
12
What does this print?
fn make() -> String {
let text = String::from("hello");
text
}
fn main() {
println!("{}", make());
}Running it prints:
hello
What does this print?
fn make() -> &String {
let text = String::from("hello");
&text
}
fn main() {
println!("{}", make());
}The compiler says:
error[E0106]: missing lifetime specifier
Why: The reference would point at text, which is dropped at the closing brace of make. Rust refuses the return type instead of handing you a reference to memory that is gone.
What does this print?
fn shout(text: String) -> String {
text.to_uppercase()
}
fn main() {
let name = String::from("rust");
let loud = shout(name);
println!("{loud}");
println!("{name}");
}The compiler says:
error[E0382]: borrow of moved value: `name`
What does this print?
fn show(text: &String) {
println!("{text}");
}
fn main() {
let name = String::from("ferris");
show(&name);
println!("{name}");
}Running it prints:
ferris ferris
What does this print?
fn add_mark(text: &mut String) {
text.push('!');
}
fn main() {
let mut greeting = String::from("hey");
add_mark(&mut greeting);
add_mark(&mut greeting);
println!("{greeting}");
}Running it prints:
hey!!
What does this print?
fn add_mark(text: &mut String) {
text.push('!');
}
fn main() {
let greeting = String::from("hey");
add_mark(&mut greeting);
println!("{greeting}");
}The compiler says:
error[E0596]: cannot borrow `greeting` as mutable, as it is not declared as mutable
Why: The function asks for &mut String, but greeting was not declared mut. You cannot lend out the right to change a value you did not mark as changeable.
What does this print?
fn main() {
let greeting = String::from("hey");
let a = &greeting;
let b = &greeting;
println!("{a} {b}");
}Running it prints:
hey hey
What does this print?
fn main() {
let mut greeting = String::from("hey");
let a = &greeting;
let b = &mut greeting;
println!("{a} {b}");
}The compiler says:
error[E0502]: cannot borrow `greeting` as mutable because it is also borrowed as immutable
What does this print?
fn main() {
let mut greeting = String::from("hey");
let a = &mut greeting;
let b = &mut greeting;
println!("{a} {b}");
}The compiler says:
error[E0499]: cannot borrow `greeting` as mutable more than once at a time
What does this print?
fn main() {
let mut greeting = String::from("hey");
let a = &greeting;
println!("{a}");
let b = &mut greeting;
b.push('?');
println!("{b}");
}Running it prints:
hey hey?
Why: A borrow ends after the last line that uses it. a is printed and never used again, so the &mut borrow on the next line is allowed.
What does this print?
fn main() {
let mut greeting = String::from("hey");
let a = &greeting;
greeting.push('!');
println!("{a}");
}The compiler says:
error[E0502]: cannot borrow `greeting` as mutable because it is also borrowed as immutable
Why: push changes the text, so it needs a &mut borrow of greeting. The owner is not exempt from the rules while a is still in use.
What does this print?
fn shout(text: &String) -> String {
text.to_uppercase()
}
fn main() {
let name = String::from("rust");
let loud = shout(&name);
println!("{name} {loud}");
}Running it prints:
rust RUST
Why: to_uppercase does not change the text it is called on. It builds a new String and returns it, so name is untouched.
What does this print?
fn show(text: &String) {
println!("{text}");
}
fn main() {
let name = String::from("ferris");
show(name);
}The compiler says:
error[E0308]: mismatched types
What does this print?
fn add_mark(text: &mut String) {
text.push('!');
}
fn main() {
let mut greeting = String::from("hey");
add_mark(&greeting);
println!("{greeting}");
}The compiler says:
error[E0308]: mismatched types
Why: &greeting and &mut greeting are different types. The mut has to be written at the call as well.
What does this print?
fn length(text: &String) -> usize {
text.len()
}
fn main() {
let name = String::from("ferris");
println!("{}", length(&name) + length(&name));
}Running it prints:
12
What does this print?
fn main() {
let mut greeting = String::from("hey");
let r = &mut greeting;
r.push('!');
println!("{greeting}");
}Running it prints:
hey!
What does this print?
fn main() {
let a = String::from("hi");
let b = &a;
let c = b;
println!("{a} {b} {c}");
}Running it prints:
hi hi hi
Why: A reference is copied, not moved. let c = b leaves b working, and neither of them owns the text.
What does this print?
fn main() {
let mut s = String::from("go");
{
let r = &mut s;
r.push_str("!");
}
println!("{s}");
}Running it prints:
go!
What does this print?
fn main() {
let s = String::from("hi");
let r1 = &s;
let r2 = &s;
println!("{r1} {r2}");
}Running it prints:
hi hi
What does this print?
fn main() {
let mut s = String::from("hi");
let r1 = &s;
let r2 = &mut s;
println!("{r1} {r2}");
}The compiler says:
error[E0502]: cannot borrow `s` as mutable because it is also borrowed as immutable
What does this print?
fn takes(s: String) {
println!("{s}");
}
fn main() {
let name = String::from("Ada");
takes(name);
takes(name);
}The compiler says:
error[E0382]: use of moved value: `name`
What does this print?
fn takes(s: &String) {
println!("{s}");
}
fn main() {
let name = String::from("Ada");
takes(&name);
takes(&name);
println!("{name}");
}Running it prints:
Ada Ada Ada
What does this print?
fn main() {
let a = 7;
let b = a;
let c = b;
println!("{a} {b} {c}");
}Running it prints:
7 7 7
What does this print?
fn main() {
let mut v = vec![1, 2, 3];
let first = &v[0];
v.push(4);
println!("{first}");
}The compiler says:
error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
What does this print?
fn main() {
let mut v = vec![1, 2, 3];
v.push(4);
let first = &v[0];
println!("{first}");
}Running it prints:
1
What does this print?
fn give() -> String {
let s = String::from("back");
s
}
fn main() {
let t = give();
println!("{t}");
}Running it prints:
back
What does this print?
fn main() {
let s = String::from("xy");
let n = s.len();
println!("{s} {n}");
}Running it prints:
xy 2