Skip to content

Commit 0c12fa3

Browse files
committed
feat: Add Vec exercises
1 parent f38f42f commit 0c12fa3

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed

exercises/collections/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
### Collections
2+
3+
Rust’s standard library includes a number of very useful data
4+
structures called collections. Most other data types represent one
5+
specific value, but collections can contain multiple values. Unlike
6+
the built-in array and tuple types, the data these collections point
7+
to is stored on the heap, which means the amount of data does not need
8+
to be known at compile time and can grow or shrink as the program
9+
runs.
10+
11+
This exercise will get you familiar with two fundamental data
12+
structures that are used very often in Rust programs:
13+
14+
* A *vector* allows you to store a variable number of values next to
15+
each other.
16+
* A *hash map* allows you to associate a value with a particular key.
17+
You may also know this by the names *map* in C++, *dictionary* in
18+
Python or an *associative array* in other languages.
19+
20+
[Rust book chapter](https://doc.rust-lang.org/stable/book/ch08-01-vectors.html)

exercises/collections/vec1.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// vec1.rs
2+
// Your task is to create a `Vec` which holds the exact same elements
3+
// as in the array `a`.
4+
// Make me compile and pass the test!
5+
// Execute the command `rustlings hint collections1` if you need hints.
6+
7+
// I AM NOT DONE
8+
9+
fn array_and_vec() -> ([i32; 4], Vec<i32>) {
10+
let a = [10, 20, 30, 40]; // a plain array
11+
let v = // TODO: declare your vector here with the macro for vectors
12+
13+
(a, v)
14+
}
15+
16+
#[cfg(test)]
17+
mod tests {
18+
use super::*;
19+
20+
#[test]
21+
fn test_array_and_vec_similarity() {
22+
let (a, v) = array_and_vec();
23+
assert!(a.iter().zip(v.iter()).all(|(x, y)| x == y));
24+
}
25+
}

exercises/collections/vec2.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// vec2.rs
2+
// A Vec of even numbers is given. Your task is to complete the loop
3+
// so that each number in the Vec is multiplied by 2.
4+
//
5+
// Make me pass the test!
6+
//
7+
// Execute the command `rustlings hint collections2` if you need
8+
// hints.
9+
10+
// I AM NOT DONE
11+
12+
fn vec_loop(mut v: Vec<i32>) -> Vec<i32> {
13+
for i in v.iter_mut() {
14+
// TODO: Fill this up so that each element in the Vec `v` is
15+
// multiplied by 2.
16+
}
17+
18+
// At this point, `v` should be equal to [4, 8, 12, 16, 20].
19+
v
20+
}
21+
22+
#[cfg(test)]
23+
mod tests {
24+
use super::*;
25+
26+
#[test]
27+
fn test_vec_loop() {
28+
let v: Vec<i32> = (1..).filter(|x| x % 2 == 0).take(5).collect();
29+
let ans = vec_loop(v.clone());
30+
31+
assert_eq!(
32+
ans,
33+
v.iter()
34+
.map(|x| x * 2)
35+
.collect::<Vec<i32>>()
36+
);
37+
}
38+
}

info.toml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,36 @@ its internal structure (the `fruits` and `veggies` modules and
370370
associated constants). It's almost there except for one keyword missing for
371371
each constant."""
372372

373+
# COLLECTIONS
374+
375+
[[exercises]]
376+
name = "collections1"
377+
path = "exercises/collections/vec1.rs"
378+
mode = "test"
379+
hint = """
380+
In Rust, there are two ways to define a Vector.
381+
382+
1. One way is to use the `Vec::new()` function to create a new vector
383+
and fill it with the `push()` method.
384+
385+
2. The second way, which is simpler is to use the `vec![]` macro and
386+
define your elements inside the square brackets.
387+
388+
Check this chapter: https://doc.rust-lang.org/stable/book/ch08-01-vectors.html
389+
of the Rust book to learn more.
390+
"""
391+
392+
[[exercises]]
393+
name = "collections2"
394+
path = "exercises/collections/vec2.rs"
395+
mode = "test"
396+
hint = """
397+
Hint 1: `i` is each element from the Vec as they are being iterated.
398+
Can you try multiplying this?
399+
400+
Hint 2: Check the suggestion from the compiler error ;)
401+
"""
402+
373403
# MACROS
374404

375405
[[exercises]]

0 commit comments

Comments
 (0)