initial commit
This commit is contained in:
17
vecs/README.md
Normal file
17
vecs/README.md
Normal file
@@ -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/stable/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)
|
||||
28
vecs/vecs1.rs
Normal file
28
vecs/vecs1.rs
Normal file
@@ -0,0 +1,28 @@
|
||||
// vecs1.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 `rustlings hint vecs1` or use the `hint` watch subcommand for a hint.
|
||||
|
||||
// I AM DONE
|
||||
|
||||
fn array_and_vec() -> ([i32; 4], Vec<i32>) {
|
||||
let a = [10, 20, 30, 40]; // a plain array
|
||||
let v = vec![10, 20, 30, 40]; // 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_eq!(a, v[..]);
|
||||
}
|
||||
}
|
||||
46
vecs/vecs2.rs
Normal file
46
vecs/vecs2.rs
Normal file
@@ -0,0 +1,46 @@
|
||||
// vecs2.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 `rustlings hint vecs2` or use the `hint` watch subcommand for a hint.
|
||||
|
||||
// I AM DONE
|
||||
|
||||
fn vec_loop(mut v: Vec<i32>) -> Vec<i32> {
|
||||
for element in v.iter_mut() {
|
||||
// TODO: Fill this up so that each element in the Vec `v` is
|
||||
// multiplied by 2.
|
||||
*element *= 2;
|
||||
}
|
||||
|
||||
// At this point, `v` should be equal to [4, 8, 12, 16, 20].
|
||||
v
|
||||
}
|
||||
|
||||
fn vec_map(v: &Vec<i32>) -> Vec<i32> {
|
||||
v.iter().map(|element| *element * 2).collect()
|
||||
}
|
||||
|
||||
#[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>>());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_vec_map() {
|
||||
let v: Vec<i32> = (1..).filter(|x| x % 2 == 0).take(5).collect();
|
||||
let ans = vec_map(&v);
|
||||
|
||||
assert_eq!(ans, v.iter().map(|x| x * 2).collect::<Vec<i32>>());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user