Remove move_semantics4, add rest of move_semantics solutions

This commit is contained in:
mo8it 2024-06-21 18:14:19 +02:00
parent fd558065c7
commit e4dbbbf5f5
7 changed files with 73 additions and 84 deletions

View file

@ -1,31 +1,18 @@
// Refactor this code so that instead of passing `vec0` into the `fill_vec`
// function, the Vector gets created in the function itself and passed back to
// the test function.
// `fill_vec()` no longer takes `vec: Vec<i32>` as argument - don't change this!
fn fill_vec() -> Vec<i32> {
// Instead, let's create and fill the Vec in here - how do you do that?
let mut vec = vec;
vec.push(88);
vec
}
fn main() { fn main() {
// You can optionally experiment here. // You can optionally experiment here.
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; // TODO: Fix the compiler errors only by reordering the lines in the test.
// Don't add, change or remove any line.
#[test] #[test]
fn move_semantics4() { fn move_semantics5() {
let vec0 = vec![22, 44, 66]; let mut x = 100;
let y = &mut x;
let vec1 = fill_vec(vec0); let z = &mut x;
*y += 100;
assert_eq!(vec1, vec![22, 44, 66, 88]); *z += 1000;
assert_eq!(x, 1200);
} }
} }

View file

@ -1,21 +1,22 @@
// Make me compile only by reordering the lines in the test, but without adding, // TODO: Fix the compiler erros. Don't change anything except adding or removing
// changing or removing any of them. // references (the character `&`).
fn main() { fn main() {
// You can optionally experiment here. let data = "Rust is great!".to_string();
get_char(data);
string_uppercase(&data);
} }
#[cfg(test)] // Shouldn't take ownership
mod tests { fn get_char(data: String) -> char {
use super::*; data.chars().last().unwrap()
}
#[test] // Should take ownership
fn move_semantics5() { fn string_uppercase(mut data: &String) {
let mut x = 100; data = &data.to_uppercase();
let y = &mut x;
let z = &mut x; println!("{data}");
*y += 100;
*z += 1000;
assert_eq!(x, 1200);
}
} }

View file

@ -1,21 +0,0 @@
// You can't change anything except adding or removing references.
fn main() {
let data = "Rust is great!".to_string();
get_char(data);
string_uppercase(&data);
}
// Should not take ownership
fn get_char(data: String) -> char {
data.chars().last().unwrap()
}
// Should take ownership
fn string_uppercase(mut data: &String) {
data = &data.to_uppercase();
println!("{}", data);
}

View file

@ -371,28 +371,15 @@ an existing binding to be a mutable binding instead of an immutable one :)"""
name = "move_semantics4" name = "move_semantics4"
dir = "06_move_semantics" dir = "06_move_semantics"
hint = """ hint = """
Stop reading whenever you feel like you have enough direction :) Or try
doing one step and then fixing the compiler errors that result!
So the end goal is to:
- get rid of the first line in main that creates the new vector
- so then `vec0` doesn't exist, so we can't pass it to `fill_vec`
- `fill_vec` has had its signature changed, which our call should reflect
- since we're not creating a new vec in `main` anymore, we need to create
a new vec in `fill_vec`, and fill it with the expected values"""
[[exercises]]
name = "move_semantics5"
dir = "06_move_semantics"
hint = """
Carefully reason about the range in which each mutable reference is in Carefully reason about the range in which each mutable reference is in
scope. Does it help to update the value of referent (`x`) immediately after scope. Does it help to update the value of referent (`x`) immediately after
the mutable reference is taken? Read more about 'Mutable References' the mutable reference is taken?
in the book's section 'References and Borrowing': Read more about 'Mutable References' in the book's section 'References and Borrowing':
https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#mutable-references. https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#mutable-references.
""" """
[[exercises]] [[exercises]]
name = "move_semantics6" name = "move_semantics5"
dir = "06_move_semantics" dir = "06_move_semantics"
test = false test = false
hint = """ hint = """
@ -401,14 +388,10 @@ https://doc.rust-lang.org/stable/book/ch04-02-references-and-borrowing.html
The first problem is that `get_char` is taking ownership of the string. So The first problem is that `get_char` is taking ownership of the string. So
`data` is moved and can't be used for `string_uppercase`. `data` is moved to `data` is moved and can't be used for `string_uppercase`. `data` is moved to
`get_char` first, meaning that `string_uppercase` cannot manipulate the data. `get_char` first, meaning that `string_uppercase` can't manipulate the data.
Once you've fixed that, `string_uppercase`'s function signature will also need Once you've fixed that, `string_uppercase`'s function signature will also need
to be adjusted. to be adjusted."""
Can you figure out how?
Another hint: it has to do with the `&` character."""
# STRUCTS # STRUCTS

View file

@ -1 +1,21 @@
// Solutions will be available before the stable release. Thank you for testing the beta version 🥰 fn main() {
// You can optionally experiment here.
}
#[cfg(test)]
mod tests {
// TODO: Fix the compiler errors only by reordering the lines in the test.
// Don't add, change or remove any line.
#[test]
fn move_semantics5() {
let mut x = 100;
let y = &mut x;
// `y` used here.
*y += 100;
// The mutable reference `y` is not used anymore,
// therefore a new reference can be created.
let z = &mut x;
*z += 1000;
assert_eq!(x, 1200);
}
}

View file

@ -1 +1,21 @@
// Solutions will be available before the stable release. Thank you for testing the beta version 🥰 fn main() {
let data = "Rust is great!".to_string();
get_char(&data);
string_uppercase(data);
}
// Borrows instead of taking ownership.
// It is recommended to use `&str` instead of `&String` here. But this is
// enough for now because we didn't handle strings yet.
fn get_char(data: &String) -> char {
data.chars().last().unwrap()
}
// Takes ownership instead of borrowing.
fn string_uppercase(mut data: String) {
data = data.to_uppercase();
println!("{data}");
}

View file

@ -1 +0,0 @@
// Solutions will be available before the stable release. Thank you for testing the beta version 🥰