mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-25 23:10:30 +00:00
vecs1 solution
This commit is contained in:
parent
0abcdeed42
commit
a9f0c7bf1f
3 changed files with 32 additions and 11 deletions
|
@ -1,11 +1,9 @@
|
|||
// 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!
|
||||
|
||||
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
|
||||
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)
|
||||
}
|
||||
|
@ -21,6 +19,6 @@ mod tests {
|
|||
#[test]
|
||||
fn test_array_and_vec_similarity() {
|
||||
let (a, v) = array_and_vec();
|
||||
assert_eq!(a, v[..]);
|
||||
assert_eq!(a, *v);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -307,8 +307,9 @@ 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.
|
||||
2. The second way is to use the `vec![]` macro and define your elements
|
||||
inside the square brackets. This way is simpler when you exactly know
|
||||
the initial values.
|
||||
|
||||
Check this chapter: https://doc.rust-lang.org/stable/book/ch08-01-vectors.html
|
||||
of the Rust book to learn more.
|
||||
|
|
|
@ -1 +1,23 @@
|
|||
// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
|
||||
fn array_and_vec() -> ([i32; 4], Vec<i32>) {
|
||||
let a = [10, 20, 30, 40]; // Array
|
||||
|
||||
// Used the `vec!` macro.
|
||||
let v = vec![10, 20, 30, 40];
|
||||
|
||||
(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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue