summaryrefslogtreecommitdiff
path: root/rustlings/exercises/05_vecs
diff options
context:
space:
mode:
Diffstat (limited to 'rustlings/exercises/05_vecs')
-rw-r--r--rustlings/exercises/05_vecs/README.md17
-rw-r--r--rustlings/exercises/05_vecs/vecs1.rs24
-rw-r--r--rustlings/exercises/05_vecs/vecs2.rs60
3 files changed, 101 insertions, 0 deletions
diff --git a/rustlings/exercises/05_vecs/README.md b/rustlings/exercises/05_vecs/README.md
new file mode 100644
index 0000000..e1b6128
--- /dev/null
+++ b/rustlings/exercises/05_vecs/README.md
@@ -0,0 +1,17 @@
+# Vectors
+
+Vectors are one of the most-used Rust data structures. In other programming
+languages, they'd simply be called Arrays, but since Rust operates on a
+bit of a lower level, an array in Rust is stored on the stack (meaning it
+can't grow or shrink, and the size needs to be known at compile time),
+and a Vector is stored in the heap (where these restrictions do not apply).
+
+Vectors are a bit of a later chapter in the book, but we think that they're
+useful enough to talk about them a bit earlier. We shall be talking about
+the other useful data structure, hash maps, later.
+
+## Further information
+
+- [Storing Lists of Values with Vectors](https://doc.rust-lang.org/book/ch08-01-vectors.html)
+- [`iter_mut`](https://doc.rust-lang.org/std/primitive.slice.html#method.iter_mut)
+- [`map`](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.map)
diff --git a/rustlings/exercises/05_vecs/vecs1.rs b/rustlings/exercises/05_vecs/vecs1.rs
new file mode 100644
index 0000000..68e1aff
--- /dev/null
+++ b/rustlings/exercises/05_vecs/vecs1.rs
@@ -0,0 +1,24 @@
+fn array_and_vec() -> ([i32; 4], Vec<i32>) {
+ let a = [10, 20, 30, 40]; // Array
+
+ // TODO: Create a vector called `v` which contains the exact same elements as in the array `a`.
+ // Use the vector macro.
+ // let v = ???;
+
+ (a, v)
+}
+
+fn main() {
+ // You can optionally experiment here.
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_array_and_vec_similarity() {
+ let (a, v) = array_and_vec();
+ assert_eq!(a, *v);
+ }
+}
diff --git a/rustlings/exercises/05_vecs/vecs2.rs b/rustlings/exercises/05_vecs/vecs2.rs
new file mode 100644
index 0000000..a9be258
--- /dev/null
+++ b/rustlings/exercises/05_vecs/vecs2.rs
@@ -0,0 +1,60 @@
+fn vec_loop(input: &[i32]) -> Vec<i32> {
+ let mut output = Vec::new();
+
+ for element in input {
+ // TODO: Multiply each element in the `input` slice by 2 and push it to
+ // the `output` vector.
+ }
+
+ output
+}
+
+fn vec_map_example(input: &[i32]) -> Vec<i32> {
+ // An example of collecting a vector after mapping.
+ // We map each element of the `input` slice to its value plus 1.
+ // If the input is `[1, 2, 3]`, the output is `[2, 3, 4]`.
+ input.iter().map(|element| element + 1).collect()
+}
+
+fn vec_map(input: &[i32]) -> Vec<i32> {
+ // TODO: Here, we also want to multiply each element in the `input` slice
+ // by 2, but with iterator mapping instead of manually pushing into an empty
+ // vector.
+ // See the example in the function `vec_map_example` above.
+ input
+ .iter()
+ .map(|element| {
+ // ???
+ })
+ .collect()
+}
+
+fn main() {
+ // You can optionally experiment here.
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_vec_loop() {
+ let input = [2, 4, 6, 8, 10];
+ let ans = vec_loop(&input);
+ assert_eq!(ans, [4, 8, 12, 16, 20]);
+ }
+
+ #[test]
+ fn test_vec_map_example() {
+ let input = [1, 2, 3];
+ let ans = vec_map_example(&input);
+ assert_eq!(ans, [2, 3, 4]);
+ }
+
+ #[test]
+ fn test_vec_map() {
+ let input = [2, 4, 6, 8, 10];
+ let ans = vec_map(&input);
+ assert_eq!(ans, [4, 8, 12, 16, 20]);
+ }
+}