From 946c29679e27433ff455bdb30343551757d87769 Mon Sep 17 00:00:00 2001 From: mo8it Date: Fri, 21 Jun 2024 16:16:52 +0200 Subject: [PATCH] move_semantics1 solution --- .../06_move_semantics/move_semantics1.rs | 3 +-- rustlings-macros/info.toml | 3 +-- .../06_move_semantics/move_semantics1.rs | 26 ++++++++++++++++++- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/exercises/06_move_semantics/move_semantics1.rs b/exercises/06_move_semantics/move_semantics1.rs index 8c3fe3a7..4eb3d618 100644 --- a/exercises/06_move_semantics/move_semantics1.rs +++ b/exercises/06_move_semantics/move_semantics1.rs @@ -1,3 +1,4 @@ +// TODO: Fix the compiler error in this function. fn fill_vec(vec: Vec) -> Vec { let vec = vec; @@ -17,9 +18,7 @@ mod tests { #[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-macros/info.toml b/rustlings-macros/info.toml index 3edd1c67..bfe32cdd 100644 --- a/rustlings-macros/info.toml +++ b/rustlings-macros/info.toml @@ -342,8 +342,7 @@ error on the line where we push an element to the vector, right? The fix for this is going to be adding one keyword, and the addition is NOT on the line where we push to the vector (where the error is). -Also: Try accessing `vec0` after having called `fill_vec()`. See what -happens!""" +Try accessing `vec0` after having called `fill_vec()`. See what happens!""" [[exercises]] name = "move_semantics2" diff --git a/solutions/06_move_semantics/move_semantics1.rs b/solutions/06_move_semantics/move_semantics1.rs index 4e181989..ac34e7a0 100644 --- a/solutions/06_move_semantics/move_semantics1.rs +++ b/solutions/06_move_semantics/move_semantics1.rs @@ -1 +1,25 @@ -// Solutions will be available before the stable release. Thank you for testing the beta version 🥰 +fn fill_vec(vec: Vec) -> Vec { + 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]); + } +}