summaryrefslogtreecommitdiff
path: root/rust/rustlings/solutions/06_move_semantics
diff options
context:
space:
mode:
authorHunter Kvalevog <hunter@kvog.sh>2026-10-02 21:53:51 -0500
committerHunter Kvalevog <hunter@kvog.sh>2026-10-02 21:53:51 -0500
commit4dfc11d74ab02ac5765ae5d08fb750c91f4744b4 (patch)
tree17ba62056d6b691dd0217b0e8f1b3a5826dce0f6 /rust/rustlings/solutions/06_move_semantics
parent02d8f1c62896576e1266553a56980eddbc2a88d7 (diff)
rustlings: move to rust/
Diffstat (limited to 'rust/rustlings/solutions/06_move_semantics')
-rw-r--r--rust/rustlings/solutions/06_move_semantics/move_semantics1.rs25
-rw-r--r--rust/rustlings/solutions/06_move_semantics/move_semantics2.rs28
-rw-r--r--rust/rustlings/solutions/06_move_semantics/move_semantics3.rs22
-rw-r--r--rust/rustlings/solutions/06_move_semantics/move_semantics4.rs19
-rw-r--r--rust/rustlings/solutions/06_move_semantics/move_semantics5.rs23
5 files changed, 117 insertions, 0 deletions
diff --git a/rust/rustlings/solutions/06_move_semantics/move_semantics1.rs b/rust/rustlings/solutions/06_move_semantics/move_semantics1.rs
new file mode 100644
index 0000000..ac34e7a
--- /dev/null
+++ b/rust/rustlings/solutions/06_move_semantics/move_semantics1.rs
@@ -0,0 +1,25 @@
+fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
+ let mut vec = vec;
+ // ^^^ added
+
+ 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);
+ // `vec0` can't be accessed anymore because it is moved to `fill_vec`.
+ assert_eq!(vec1, vec![22, 44, 66, 88]);
+ }
+}
diff --git a/rust/rustlings/solutions/06_move_semantics/move_semantics2.rs b/rust/rustlings/solutions/06_move_semantics/move_semantics2.rs
new file mode 100644
index 0000000..7bcd33a
--- /dev/null
+++ b/rust/rustlings/solutions/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::*;
+
+ #[test]
+ fn move_semantics2() {
+ let vec0 = vec![22, 44, 66];
+
+ // Cloning `vec0` so that the clone is moved into `fill_vec`, not `vec0`
+ // itself.
+ let vec1 = fill_vec(vec0.clone());
+
+ assert_eq!(vec0, [22, 44, 66]);
+ assert_eq!(vec1, [22, 44, 66, 88]);
+ }
+}
diff --git a/rust/rustlings/solutions/06_move_semantics/move_semantics3.rs b/rust/rustlings/solutions/06_move_semantics/move_semantics3.rs
new file mode 100644
index 0000000..7ba4006
--- /dev/null
+++ b/rust/rustlings/solutions/06_move_semantics/move_semantics3.rs
@@ -0,0 +1,22 @@
+fn fill_vec(mut vec: Vec<i32>) -> Vec<i32> {
+ // ^^^ added
+ 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/rust/rustlings/solutions/06_move_semantics/move_semantics4.rs b/rust/rustlings/solutions/06_move_semantics/move_semantics4.rs
new file mode 100644
index 0000000..1a39d4f
--- /dev/null
+++ b/rust/rustlings/solutions/06_move_semantics/move_semantics4.rs
@@ -0,0 +1,19 @@
+fn main() {
+ // You can optionally experiment here.
+}
+
+#[cfg(test)]
+mod tests {
+ #[test]
+ fn move_semantics4() {
+ let mut x = Vec::new();
+ let y = &mut x;
+ // `y` used here.
+ y.push(42);
+ // The mutable reference `y` is not used anymore,
+ // therefore a new reference can be created.
+ let z = &mut x;
+ z.push(13);
+ assert_eq!(x, [42, 13]);
+ }
+}
diff --git a/rust/rustlings/solutions/06_move_semantics/move_semantics5.rs b/rust/rustlings/solutions/06_move_semantics/move_semantics5.rs
new file mode 100644
index 0000000..1410e91
--- /dev/null
+++ b/rust/rustlings/solutions/06_move_semantics/move_semantics5.rs
@@ -0,0 +1,23 @@
+#![allow(clippy::ptr_arg)]
+
+// 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}");
+}
+
+fn main() {
+ let data = "Rust is great!".to_string();
+
+ get_char(&data);
+
+ string_uppercase(data);
+}