Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map on a vec and use a closure with pattern matching in Rust

I'd like to use map to iterate over an array and do stuff per item and get rid of the for loop. An error which I do not understand blocks my attempt. What I want to achieve is to iterate through a vector of i32 and match on them to concat a string with string literals and then return it at the end.

Function:

pub fn convert_to_rainspeak(prime_factors: Vec<i32>) -> String {
    let mut speak = String::new();
    prime_factors.iter().map(|&factor| {
        match factor {
            3 => { speak.push_str("Pling"); },
            5 => { speak.push_str("Plang"); },
            7 => { speak.push_str("Plong"); },
            _ => {}
        }
    }).collect();
    speak
}

fn main() {}

Output:

error[E0282]: type annotations needed
  --> src/main.rs:10:8
   |
10 |     }).collect();
   |        ^^^^^^^ cannot infer type for `B`
like image 933
xetra11 Avatar asked Oct 18 '25 19:10

xetra11


1 Answers

Iterator::collect is defined as:

fn collect<B>(self) -> B 
where
    B: FromIterator<Self::Item>

That is, it returns a type that is up to the caller. However, you have completely disregarded the output, so there's no way for it to infer a type. The code misuses collect when it basically wants to use for.

In your "fixed" version (which has since been edited, making this paragraph make no sense), you are being very inefficient by allocating a string in every iteration. Plus you don't need to specify any explicit types other than those on the function, and you should accept a &[i32] instead:

fn convert_to_rainspeak(prime_factors: &[i32]) -> String {
    prime_factors.iter()
        .map(|&factor| {
            match factor {
                3 => "Pling",
                5 => "Plang",
                7 => "Plong",
                _ => "",
            }
        })
        .collect()
}

fn main() {
    println!("{}", convert_to_rainspeak(&[1, 2, 3, 4, 5]));
}
like image 116
Shepmaster Avatar answered Oct 20 '25 16:10

Shepmaster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!