DEV Community

loading...

Discussion on: Daily Challenge #172 - Find All in an Array

Collapse
ciarant profile image
Ciaran Treanor

Rust:

fn find_all(numbers: Vec<usize>, value: usize) -> Vec<usize> {
    numbers.iter()
        .enumerate()
        .filter(|&(_, val)| *val == value)
        .map(|(index, _)| index)
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test1() {
        assert_eq!(vec![2, 4], find_all(vec![6, 9, 3, 4, 3, 82, 1], 3));
        assert_eq!(vec![1,9], find_all(vec![10, 16, 20, 6, 14, 11, 20, 2, 17, 16, 14], 16));
        assert_eq!(vec![0,1,8], find_all(vec![20, 20, 10, 13, 15, 2, 7, 2, 20, 3, 18, 2, 3, 2, 16, 10, 9, 9, 7, 5, 15, 5], 20));
    }
}