#Rustlings - exercises - vecs2.rs

1 messages · Page 1 of 1 (latest)

fallow dirge
#

Oh no

#

I just deleted my whole question...

#

Ok, so i've been doing the Rustlings to learn Rust and basically I'm stuck at the vecs2.rs exercise, here's the code I have so far (I managed to do the first half, but I can't wrap my head around the second half

// 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 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.
        *i *= 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(|num| {
        // TODO: Do the same thing as above - but instead of mutating the
        // Vec, you can just return the new number!
        ???
    }).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>>());
    }
}

here's the error in the console

⚠️  Compiling of exercises/vecs/vecs2.rs failed! Please try again. Here's the output:
error: expected expression, found `?`
  --> exercises/vecs/vecs2.rs:26:9
   |
26 |         ???
   |         ^ expected expression

error: aborting due to previous error
#

I'm using gitpod if that has an impact on anything

dense gulch
#

I dont know what to explain better than rustlings already does there. I guess you can look at the documentation of the functions there, specifically map, to understand what supposed to happen better