Practice 06

Ownership and Borrowing

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.

Course

0 of 50 answered·0 correct

Moves and Copies

1.

What does this print?

fn main() {
    let a = String::from("hi");
    let b = a;

    println!("{b}");
}
2.

What does this print?

fn main() {
    let a = String::from("hi");
    let b = a;

    println!("{a}");
}
3.

What does this print?

fn main() {
    let a = 5;
    let b = a;

    println!("{a} {b}");
}
4.

What does this print?

fn main() {
    let a = (1, 2);
    let b = a;

    println!("{} {}", a.0, b.1);
}
5.

What does this print?

fn main() {
    let a = (1, String::from("x"));
    let b = a;

    println!("{}", a.0);
}
6.

What does this print?

fn main() {
    let flag = true;
    let copy = flag;

    println!("{flag} {copy}");
}
7.

What does this print?

fn main() {
    let letter = 'R';
    let copy = letter;

    println!("{letter}{copy}");
}
8.

What does this print?

fn main() {
    let count = 3;
    let count = count + 1;

    println!("{count}");
}

clone, len and Text

9.

What does this print?

fn main() {
    let a = String::from("hi");
    let b = a.clone();

    println!("{a} {b}");
}
10.

What does this print?

fn main() {
    let text = String::from("abc");

    println!("{}", text.len());
}
11.

What does this print?

fn main() {
    let text = String::from("a b c");

    println!("{}", text.len());
}
12.

What does this print?

fn main() {
    let mut text = String::from("ab");
    text.push_str("cd");

    println!("{} {}", text, text.len());
}
13.

What does this print?

fn main() {
    let a = String::from("hi");
    let b = a.clone();

    println!("{}", a.len() + b.len());
}
14.

What does this print?

fn main() {
    let mut text = String::from("one");
    text = String::from("two");

    println!("{text}");
}
15.

What does this print?

fn main() {
    let a = String::from("hi");
    let b = a.clone();
    let c = b;

    println!("{a} {c}");
}
16.

What does this print?

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

fn main() {
    let name = String::from("ferris");

    println!("{}", take(name.clone()));
    println!("{name}");
}

Scope and Blocks

17.

What does this print?

fn main() {
    {
        let inside = String::from("hello");
        println!("{inside}");
    }

    println!("done");
}
18.

What does this print?

fn main() {
    {
        let inside = String::from("hello");
    }

    println!("{inside}");
}
19.

What does this print?

fn main() {
    let outer = String::from("out");

    {
        let inner = &outer;
        println!("{inner}");
    }

    println!("{outer}");
}
20.

What does this print?

fn main() {
    let text = String::from("hi");

    {
        let moved = text;
        println!("{moved}");
    }

    println!("{text}");
}

Functions and Ownership

21.

What does this print?

fn show(text: String) {
    println!("{text}");
}

fn main() {
    let name = String::from("ferris");

    show(name);
    println!("{name}");
}
22.

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}");
}
23.

What does this print?

fn double(n: i32) -> i32 {
    n * 2
}

fn main() {
    let count = 4;

    println!("{}", double(count) + count);
}
24.

What does this print?

fn make() -> String {
    let text = String::from("hello");
    text
}

fn main() {
    println!("{}", make());
}
25.

What does this print?

fn make() -> &String {
    let text = String::from("hello");
    &text
}

fn main() {
    println!("{}", make());
}
26.

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}");
}

Borrowing

27.

What does this print?

fn show(text: &String) {
    println!("{text}");
}

fn main() {
    let name = String::from("ferris");

    show(&name);
    println!("{name}");
}
28.

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}");
}
29.

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}");
}
30.

What does this print?

fn main() {
    let greeting = String::from("hey");

    let a = &greeting;
    let b = &greeting;

    println!("{a} {b}");
}
31.

What does this print?

fn main() {
    let mut greeting = String::from("hey");

    let a = &greeting;
    let b = &mut greeting;

    println!("{a} {b}");
}
32.

What does this print?

fn main() {
    let mut greeting = String::from("hey");

    let a = &mut greeting;
    let b = &mut greeting;

    println!("{a} {b}");
}
33.

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}");
}
34.

What does this print?

fn main() {
    let mut greeting = String::from("hey");

    let a = &greeting;
    greeting.push('!');

    println!("{a}");
}
35.

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}");
}
36.

What does this print?

fn show(text: &String) {
    println!("{text}");
}

fn main() {
    let name = String::from("ferris");

    show(name);
}
37.

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}");
}
38.

What does this print?

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

fn main() {
    let name = String::from("ferris");

    println!("{}", length(&name) + length(&name));
}
39.

What does this print?

fn main() {
    let mut greeting = String::from("hey");

    let r = &mut greeting;
    r.push('!');

    println!("{greeting}");
}
40.

What does this print?

fn main() {
    let a = String::from("hi");
    let b = &a;
    let c = b;

    println!("{a} {b} {c}");
}
41.

What does this print?

fn main() {
    let mut s = String::from("go");
    {
        let r = &mut s;
        r.push_str("!");
    }
    println!("{s}");
}
42.

What does this print?

fn main() {
    let s = String::from("hi");
    let r1 = &s;
    let r2 = &s;
    println!("{r1} {r2}");
}
43.

What does this print?

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

What does this print?

fn takes(s: String) {
    println!("{s}");
}

fn main() {
    let name = String::from("Ada");
    takes(name);
    takes(name);
}
45.

What does this print?

fn takes(s: &String) {
    println!("{s}");
}

fn main() {
    let name = String::from("Ada");
    takes(&name);
    takes(&name);
    println!("{name}");
}
46.

What does this print?

fn main() {
    let a = 7;
    let b = a;
    let c = b;
    println!("{a} {b} {c}");
}
47.

What does this print?

fn main() {
    let mut v = vec![1, 2, 3];
    let first = &v[0];
    v.push(4);
    println!("{first}");
}
48.

What does this print?

fn main() {
    let mut v = vec![1, 2, 3];
    v.push(4);
    let first = &v[0];
    println!("{first}");
}
49.

What does this print?

fn give() -> String {
    let s = String::from("back");
    s
}

fn main() {
    let t = give();
    println!("{t}");
}
50.

What does this print?

fn main() {
    let s = String::from("xy");
    let n = s.len();
    println!("{s} {n}");
}