mirror of
https://github.com/notohh/rustlings.git
synced 2024-11-21 13:32:23 -05:00
Remove move_semantics4, add rest of move_semantics solutions
This commit is contained in:
parent
fd558065c7
commit
e4dbbbf5f5
7 changed files with 73 additions and 84 deletions
|
@ -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() {
|
||||
// You can optionally experiment here.
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
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]
|
||||
fn move_semantics4() {
|
||||
let vec0 = vec![22, 44, 66];
|
||||
|
||||
let vec1 = fill_vec(vec0);
|
||||
|
||||
assert_eq!(vec1, vec![22, 44, 66, 88]);
|
||||
fn move_semantics5() {
|
||||
let mut x = 100;
|
||||
let y = &mut x;
|
||||
let z = &mut x;
|
||||
*y += 100;
|
||||
*z += 1000;
|
||||
assert_eq!(x, 1200);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,21 +1,22 @@
|
|||
// Make me compile only by reordering the lines in the test, but without adding,
|
||||
// changing or removing any of them.
|
||||
// TODO: Fix the compiler erros. Don't change anything except adding or removing
|
||||
// references (the character `&`).
|
||||
|
||||
fn main() {
|
||||
// You can optionally experiment here.
|
||||
let data = "Rust is great!".to_string();
|
||||
|
||||
get_char(data);
|
||||
|
||||
string_uppercase(&data);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn move_semantics5() {
|
||||
let mut x = 100;
|
||||
let y = &mut x;
|
||||
let z = &mut x;
|
||||
*y += 100;
|
||||
*z += 1000;
|
||||
assert_eq!(x, 1200);
|
||||
}
|
||||
// Shouldn't 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}");
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
|
@ -371,28 +371,15 @@ an existing binding to be a mutable binding instead of an immutable one :)"""
|
|||
name = "move_semantics4"
|
||||
dir = "06_move_semantics"
|
||||
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
|
||||
scope. Does it help to update the value of referent (`x`) immediately after
|
||||
the mutable reference is taken? Read more about 'Mutable References'
|
||||
in the book's section 'References and Borrowing':
|
||||
the mutable reference is taken?
|
||||
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.
|
||||
"""
|
||||
|
||||
[[exercises]]
|
||||
name = "move_semantics6"
|
||||
name = "move_semantics5"
|
||||
dir = "06_move_semantics"
|
||||
test = false
|
||||
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
|
||||
`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
|
||||
to be adjusted.
|
||||
|
||||
Can you figure out how?
|
||||
|
||||
Another hint: it has to do with the `&` character."""
|
||||
to be adjusted."""
|
||||
|
||||
# STRUCTS
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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}");
|
||||
}
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
|
Loading…
Reference in a new issue