summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNidhal Messaoudi <nidhalmessaoudi@hotmail.com>2023-02-27 21:16:40 +0100
committerNidhal Messaoudi <nidhalmessaoudi@hotmail.com>2023-02-27 21:16:40 +0100
commite3a20b8bc899eae9dfb440b885836812fa8614ee (patch)
tree36778578b6c67e66405e9c1470f69b6b19d2178f
parentf7678b4a9ed03c81e2bedb998d5893f882acf915 (diff)
Learning about vectors
-rw-r--r--exercises/move_semantics/move_semantics1.rs4
-rw-r--r--exercises/move_semantics/move_semantics2.rs10
-rw-r--r--exercises/move_semantics/move_semantics3.rs4
-rw-r--r--exercises/vecs/vecs1.rs4
-rw-r--r--exercises/vecs/vecs2.rs6
5 files changed, 9 insertions, 19 deletions
diff --git a/exercises/move_semantics/move_semantics1.rs b/exercises/move_semantics/move_semantics1.rs
index aac6dfc..c60ee74 100644
--- a/exercises/move_semantics/move_semantics1.rs
+++ b/exercises/move_semantics/move_semantics1.rs
@@ -1,12 +1,10 @@
// move_semantics1.rs
// Execute `rustlings hint move_semantics1` or use the `hint` watch subcommand for a hint.
-// I AM NOT DONE
-
fn main() {
let vec0 = Vec::new();
- let vec1 = fill_vec(vec0);
+ let mut vec1 = fill_vec(vec0);
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
diff --git a/exercises/move_semantics/move_semantics2.rs b/exercises/move_semantics/move_semantics2.rs
index 6487085..229f13b 100644
--- a/exercises/move_semantics/move_semantics2.rs
+++ b/exercises/move_semantics/move_semantics2.rs
@@ -2,12 +2,10 @@
// Make me compile without changing line 13 or moving line 10!
// Execute `rustlings hint move_semantics2` or use the `hint` watch subcommand for a hint.
-// I AM NOT DONE
-
fn main() {
- let vec0 = Vec::new();
+ let mut vec0 = Vec::new();
- let mut vec1 = fill_vec(vec0);
+ let mut vec1 = fill_vec(&mut vec0);
// Do not change the following line!
println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0);
@@ -17,12 +15,12 @@ fn main() {
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
}
-fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
+fn fill_vec(vec: &mut Vec<i32>) -> Vec<i32> {
let mut vec = vec;
vec.push(22);
vec.push(44);
vec.push(66);
- vec
+ vec.to_vec()
}
diff --git a/exercises/move_semantics/move_semantics3.rs b/exercises/move_semantics/move_semantics3.rs
index eaa30e3..bb83023 100644
--- a/exercises/move_semantics/move_semantics3.rs
+++ b/exercises/move_semantics/move_semantics3.rs
@@ -3,8 +3,6 @@
// (no lines with multiple semicolons necessary!)
// Execute `rustlings hint move_semantics3` or use the `hint` watch subcommand for a hint.
-// I AM NOT DONE
-
fn main() {
let vec0 = Vec::new();
@@ -17,7 +15,7 @@ fn main() {
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
}
-fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
+fn fill_vec(mut vec: Vec<i32>) -> Vec<i32> {
vec.push(22);
vec.push(44);
vec.push(66);
diff --git a/exercises/vecs/vecs1.rs b/exercises/vecs/vecs1.rs
index 4e8c4cb..0e8e2f7 100644
--- a/exercises/vecs/vecs1.rs
+++ b/exercises/vecs/vecs1.rs
@@ -4,11 +4,9 @@
// Make me compile and pass the test!
// Execute `rustlings hint vecs1` or use the `hint` watch subcommand for a hint.
-// I AM NOT DONE
-
fn array_and_vec() -> ([i32; 4], Vec<i32>) {
let a = [10, 20, 30, 40]; // a plain array
- let v = // TODO: declare your vector here with the macro for vectors
+ let v = vec![10, 20, 30, 40]; // TODO: declare your vector here with the macro for vectors
(a, v)
}
diff --git a/exercises/vecs/vecs2.rs b/exercises/vecs/vecs2.rs
index 5bea09a..d016daa 100644
--- a/exercises/vecs/vecs2.rs
+++ b/exercises/vecs/vecs2.rs
@@ -6,13 +6,11 @@
//
// Execute `rustlings hint vecs2` or use the `hint` watch subcommand for a hint.
-// I AM NOT DONE
-
fn vec_loop(mut v: Vec<i32>) -> Vec<i32> {
for i in v.iter_mut() {
// TODO: Fill this up so that each element in the Vec `v` is
// multiplied by 2.
- ???
+ *i *= 2;
}
// At this point, `v` should be equal to [4, 8, 12, 16, 20].
@@ -23,7 +21,7 @@ fn vec_map(v: &Vec<i32>) -> Vec<i32> {
v.iter().map(|num| {
// TODO: Do the same thing as above - but instead of mutating the
// Vec, you can just return the new number!
- ???
+ num * 2
}).collect()
}