feat: Add Vec exercises
This commit is contained in:
parent
f38f42f17d
commit
0c12fa31c5
20
exercises/collections/README.md
Normal file
20
exercises/collections/README.md
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
### Collections
|
||||||
|
|
||||||
|
Rust’s standard library includes a number of very useful data
|
||||||
|
structures called collections. Most other data types represent one
|
||||||
|
specific value, but collections can contain multiple values. Unlike
|
||||||
|
the built-in array and tuple types, the data these collections point
|
||||||
|
to is stored on the heap, which means the amount of data does not need
|
||||||
|
to be known at compile time and can grow or shrink as the program
|
||||||
|
runs.
|
||||||
|
|
||||||
|
This exercise will get you familiar with two fundamental data
|
||||||
|
structures that are used very often in Rust programs:
|
||||||
|
|
||||||
|
* A *vector* allows you to store a variable number of values next to
|
||||||
|
each other.
|
||||||
|
* A *hash map* allows you to associate a value with a particular key.
|
||||||
|
You may also know this by the names *map* in C++, *dictionary* in
|
||||||
|
Python or an *associative array* in other languages.
|
||||||
|
|
||||||
|
[Rust book chapter](https://doc.rust-lang.org/stable/book/ch08-01-vectors.html)
|
25
exercises/collections/vec1.rs
Normal file
25
exercises/collections/vec1.rs
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
// vec1.rs
|
||||||
|
// Your task is to create a `Vec` which holds the exact same elements
|
||||||
|
// as in the array `a`.
|
||||||
|
// Make me compile and pass the test!
|
||||||
|
// Execute the command `rustlings hint collections1` if you need hints.
|
||||||
|
|
||||||
|
// 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
|
||||||
|
|
||||||
|
(a, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_array_and_vec_similarity() {
|
||||||
|
let (a, v) = array_and_vec();
|
||||||
|
assert!(a.iter().zip(v.iter()).all(|(x, y)| x == y));
|
||||||
|
}
|
||||||
|
}
|
38
exercises/collections/vec2.rs
Normal file
38
exercises/collections/vec2.rs
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
// vec2.rs
|
||||||
|
// A Vec of even numbers is given. Your task is to complete the loop
|
||||||
|
// so that each number in the Vec is multiplied by 2.
|
||||||
|
//
|
||||||
|
// Make me pass the test!
|
||||||
|
//
|
||||||
|
// Execute the command `rustlings hint collections2` if you need
|
||||||
|
// hints.
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
}
|
||||||
|
|
||||||
|
// At this point, `v` should be equal to [4, 8, 12, 16, 20].
|
||||||
|
v
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_vec_loop() {
|
||||||
|
let v: Vec<i32> = (1..).filter(|x| x % 2 == 0).take(5).collect();
|
||||||
|
let ans = vec_loop(v.clone());
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
ans,
|
||||||
|
v.iter()
|
||||||
|
.map(|x| x * 2)
|
||||||
|
.collect::<Vec<i32>>()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
30
info.toml
30
info.toml
@ -370,6 +370,36 @@ its internal structure (the `fruits` and `veggies` modules and
|
|||||||
associated constants). It's almost there except for one keyword missing for
|
associated constants). It's almost there except for one keyword missing for
|
||||||
each constant."""
|
each constant."""
|
||||||
|
|
||||||
|
# COLLECTIONS
|
||||||
|
|
||||||
|
[[exercises]]
|
||||||
|
name = "collections1"
|
||||||
|
path = "exercises/collections/vec1.rs"
|
||||||
|
mode = "test"
|
||||||
|
hint = """
|
||||||
|
In Rust, there are two ways to define a Vector.
|
||||||
|
|
||||||
|
1. One way is to use the `Vec::new()` function to create a new vector
|
||||||
|
and fill it with the `push()` method.
|
||||||
|
|
||||||
|
2. The second way, which is simpler is to use the `vec![]` macro and
|
||||||
|
define your elements inside the square brackets.
|
||||||
|
|
||||||
|
Check this chapter: https://doc.rust-lang.org/stable/book/ch08-01-vectors.html
|
||||||
|
of the Rust book to learn more.
|
||||||
|
"""
|
||||||
|
|
||||||
|
[[exercises]]
|
||||||
|
name = "collections2"
|
||||||
|
path = "exercises/collections/vec2.rs"
|
||||||
|
mode = "test"
|
||||||
|
hint = """
|
||||||
|
Hint 1: `i` is each element from the Vec as they are being iterated.
|
||||||
|
Can you try multiplying this?
|
||||||
|
|
||||||
|
Hint 2: Check the suggestion from the compiler error ;)
|
||||||
|
"""
|
||||||
|
|
||||||
# MACROS
|
# MACROS
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
Loading…
Reference in New Issue
Block a user