-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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>>() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters