summaryrefslogtreecommitdiff
path: root/exercises/06_move_semantics
diff options
context:
space:
mode:
Diffstat (limited to 'exercises/06_move_semantics')
-rw-r--r--exercises/06_move_semantics/README.md10
-rw-r--r--exercises/06_move_semantics/move_semantics1.rs23
-rw-r--r--exercises/06_move_semantics/move_semantics2.rs26
-rw-r--r--exercises/06_move_semantics/move_semantics3.rs24
-rw-r--r--exercises/06_move_semantics/move_semantics4.rs29
-rw-r--r--exercises/06_move_semantics/move_semantics5.rs19
-rw-r--r--exercises/06_move_semantics/move_semantics6.rs28
7 files changed, 159 insertions, 0 deletions
diff --git a/exercises/06_move_semantics/README.md b/exercises/06_move_semantics/README.md
new file mode 100644
index 0000000..54ddd8e
--- /dev/null
+++ b/exercises/06_move_semantics/README.md
@@ -0,0 +1,10 @@
+# Move Semantics
+
+These exercises are adapted from [pnkfelix](https://github.com/pnkfelix)'s [Rust Tutorial](https://pnkfelix.github.io/rust-examples-icfp2014/) -- Thank you Felix!!!
+
+## Further information
+
+For this section, the book links are especially important.
+
+- [Ownership](https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html)
+- [Reference and borrowing](https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html)
diff --git a/exercises/06_move_semantics/move_semantics1.rs b/exercises/06_move_semantics/move_semantics1.rs
new file mode 100644
index 0000000..e063937
--- /dev/null
+++ b/exercises/06_move_semantics/move_semantics1.rs
@@ -0,0 +1,23 @@
+// move_semantics1.rs
+//
+// Execute `rustlings hint move_semantics1` or use the `hint` watch subcommand
+// for a hint.
+
+// I AM NOT DONE
+
+#[test]
+fn main() {
+ let vec0 = vec![22, 44, 66];
+
+ let vec1 = fill_vec(vec0);
+
+ assert_eq!(vec1, vec![22, 44, 66, 88]);
+}
+
+fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
+ let vec = vec;
+
+ vec.push(88);
+
+ vec
+}
diff --git a/exercises/06_move_semantics/move_semantics2.rs b/exercises/06_move_semantics/move_semantics2.rs
new file mode 100644
index 0000000..baf6bcc
--- /dev/null
+++ b/exercises/06_move_semantics/move_semantics2.rs
@@ -0,0 +1,26 @@
+// move_semantics2.rs
+//
+// Make the test pass by finding a way to keep both Vecs separate!
+//
+// Execute `rustlings hint move_semantics2` or use the `hint` watch subcommand
+// for a hint.
+
+// I AM NOT DONE
+
+#[test]
+fn main() {
+ let vec0 = vec![22, 44, 66];
+
+ let mut vec1 = fill_vec(vec0);
+
+ assert_eq!(vec0, vec![22, 44, 66]);
+ assert_eq!(vec1, vec![22, 44, 66, 88]);
+}
+
+fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
+ let mut vec = vec;
+
+ vec.push(88);
+
+ vec
+}
diff --git a/exercises/06_move_semantics/move_semantics3.rs b/exercises/06_move_semantics/move_semantics3.rs
new file mode 100644
index 0000000..7af9e69
--- /dev/null
+++ b/exercises/06_move_semantics/move_semantics3.rs
@@ -0,0 +1,24 @@
+// move_semantics3.rs
+//
+// Make me compile without adding new lines -- just changing existing lines! (no
+// lines with multiple semicolons necessary!)
+//
+// Execute `rustlings hint move_semantics3` or use the `hint` watch subcommand
+// for a hint.
+
+// I AM NOT DONE
+
+#[test]
+fn main() {
+ let vec0 = vec![22, 44, 66];
+
+ let mut vec1 = fill_vec(vec0);
+
+ assert_eq!(vec1, vec![22, 44, 66, 88]);
+}
+
+fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
+ vec.push(88);
+
+ vec
+}
diff --git a/exercises/06_move_semantics/move_semantics4.rs b/exercises/06_move_semantics/move_semantics4.rs
new file mode 100644
index 0000000..80b49db
--- /dev/null
+++ b/exercises/06_move_semantics/move_semantics4.rs
@@ -0,0 +1,29 @@
+// move_semantics4.rs
+//
+// 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 main function.
+//
+// Execute `rustlings hint move_semantics4` or use the `hint` watch subcommand
+// for a hint.
+
+// I AM NOT DONE
+
+#[test]
+fn main() {
+ let vec0 = vec![22, 44, 66];
+
+ let mut vec1 = fill_vec(vec0);
+
+ assert_eq!(vec1, vec![22, 44, 66, 88]);
+}
+
+// `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
+}
diff --git a/exercises/06_move_semantics/move_semantics5.rs b/exercises/06_move_semantics/move_semantics5.rs
new file mode 100644
index 0000000..267bdcc
--- /dev/null
+++ b/exercises/06_move_semantics/move_semantics5.rs
@@ -0,0 +1,19 @@
+// move_semantics5.rs
+//
+// Make me compile only by reordering the lines in `main()`, but without adding,
+// changing or removing any of them.
+//
+// Execute `rustlings hint move_semantics5` or use the `hint` watch subcommand
+// for a hint.
+
+// I AM NOT DONE
+
+#[test]
+fn main() {
+ let mut x = 100;
+ let y = &mut x;
+ let z = &mut x;
+ *y += 100;
+ *z += 1000;
+ assert_eq!(x, 1200);
+}
diff --git a/exercises/06_move_semantics/move_semantics6.rs b/exercises/06_move_semantics/move_semantics6.rs
new file mode 100644
index 0000000..cace4ca
--- /dev/null
+++ b/exercises/06_move_semantics/move_semantics6.rs
@@ -0,0 +1,28 @@
+// move_semantics6.rs
+//
+// You can't change anything except adding or removing references.
+//
+// Execute `rustlings hint move_semantics6` or use the `hint` watch subcommand
+// for a hint.
+
+// I AM NOT DONE
+
+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);
+}