summaryrefslogtreecommitdiff
path: root/rustlings/exercises/06_move_semantics
diff options
context:
space:
mode:
Diffstat (limited to 'rustlings/exercises/06_move_semantics')
-rw-r--r--rustlings/exercises/06_move_semantics/README.md10
-rw-r--r--rustlings/exercises/06_move_semantics/move_semantics1.rs24
-rw-r--r--rustlings/exercises/06_move_semantics/move_semantics2.rs28
-rw-r--r--rustlings/exercises/06_move_semantics/move_semantics3.rs22
-rw-r--r--rustlings/exercises/06_move_semantics/move_semantics4.rs18
-rw-r--r--rustlings/exercises/06_move_semantics/move_semantics5.rs24
6 files changed, 126 insertions, 0 deletions
diff --git a/rustlings/exercises/06_move_semantics/README.md b/rustlings/exercises/06_move_semantics/README.md
new file mode 100644
index 0000000..54ddd8e
--- /dev/null
+++ b/rustlings/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/rustlings/exercises/06_move_semantics/move_semantics1.rs b/rustlings/exercises/06_move_semantics/move_semantics1.rs
new file mode 100644
index 0000000..4eb3d61
--- /dev/null
+++ b/rustlings/exercises/06_move_semantics/move_semantics1.rs
@@ -0,0 +1,24 @@
+// TODO: Fix the compiler error in this function.
+fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
+ let vec = vec;
+
+ vec.push(88);
+
+ vec
+}
+
+fn main() {
+ // You can optionally experiment here.
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn move_semantics1() {
+ let vec0 = vec![22, 44, 66];
+ let vec1 = fill_vec(vec0);
+ assert_eq!(vec1, vec![22, 44, 66, 88]);
+ }
+}
diff --git a/rustlings/exercises/06_move_semantics/move_semantics2.rs b/rustlings/exercises/06_move_semantics/move_semantics2.rs
new file mode 100644
index 0000000..a3ab7a0
--- /dev/null
+++ b/rustlings/exercises/06_move_semantics/move_semantics2.rs
@@ -0,0 +1,28 @@
+fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
+ let mut vec = vec;
+
+ vec.push(88);
+
+ vec
+}
+
+fn main() {
+ // You can optionally experiment here.
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ // TODO: Make both vectors `vec0` and `vec1` accessible at the same time to
+ // fix the compiler error in the test.
+ #[test]
+ fn move_semantics2() {
+ let vec0 = vec![22, 44, 66];
+
+ let vec1 = fill_vec(vec0);
+
+ assert_eq!(vec0, [22, 44, 66]);
+ assert_eq!(vec1, [22, 44, 66, 88]);
+ }
+}
diff --git a/rustlings/exercises/06_move_semantics/move_semantics3.rs b/rustlings/exercises/06_move_semantics/move_semantics3.rs
new file mode 100644
index 0000000..11dbbbe
--- /dev/null
+++ b/rustlings/exercises/06_move_semantics/move_semantics3.rs
@@ -0,0 +1,22 @@
+// TODO: Fix the compiler error in the function without adding any new line.
+fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
+ vec.push(88);
+
+ vec
+}
+
+fn main() {
+ // You can optionally experiment here.
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn move_semantics3() {
+ let vec0 = vec![22, 44, 66];
+ let vec1 = fill_vec(vec0);
+ assert_eq!(vec1, [22, 44, 66, 88]);
+ }
+}
diff --git a/rustlings/exercises/06_move_semantics/move_semantics4.rs b/rustlings/exercises/06_move_semantics/move_semantics4.rs
new file mode 100644
index 0000000..56da988
--- /dev/null
+++ b/rustlings/exercises/06_move_semantics/move_semantics4.rs
@@ -0,0 +1,18 @@
+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_semantics4() {
+ let mut x = Vec::new();
+ let y = &mut x;
+ let z = &mut x;
+ y.push(42);
+ z.push(13);
+ assert_eq!(x, [42, 13]);
+ }
+}
diff --git a/rustlings/exercises/06_move_semantics/move_semantics5.rs b/rustlings/exercises/06_move_semantics/move_semantics5.rs
new file mode 100644
index 0000000..cd0dafd
--- /dev/null
+++ b/rustlings/exercises/06_move_semantics/move_semantics5.rs
@@ -0,0 +1,24 @@
+#![allow(clippy::ptr_arg)]
+
+// TODO: Fix the compiler errors without changing anything except adding or
+// removing references (the character `&`).
+
+// 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}");
+}
+
+fn main() {
+ let data = "Rust is great!".to_string();
+
+ get_char(data);
+
+ string_uppercase(&data);
+}