mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-03-31 03:29:19 +00:00
28 lines
561 B
Rust
28 lines
561 B
Rust
fn vec_loop(input: &[i32]) -> Vec<i32> {
|
|
let mut output = Vec::new();
|
|
|
|
for element in input {
|
|
// TODO: Loop through each number in `input`,
|
|
// multiply it by 2, and add it to the `output` vector.
|
|
// Hint: Use a `for` loop and the `push` method.
|
|
}
|
|
|
|
output
|
|
}
|
|
|
|
fn main() {
|
|
// You can optionally experiment here.
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_vec_loop() {
|
|
let input = [2, 4, 6, 8, 10];
|
|
let ans = vec_loop(&input);
|
|
assert_eq!(ans, [4, 8, 12, 16, 20]);
|
|
}
|
|
}
|