How to use partition on a (String, f32) collection?

I have a Vec which contains tuples (String,f32). I wanted to partition it based on my f32. Below is the sample,

fn main() {
    let a = vec![
        (String::from("B"), 1.0),
    let (p,n): (Vec<(String,f32)>, Vec<(String,f32)>) = a.iter()
        .partition(|(_,val)| *val>=0.0);
    println!("{:?}", p);

Can you please help me?

    let (p,n): (Vec<(String,f32)>, Vec<(String,f32)>) = a.into_iter()
        .partition(|&(ref string, val)| val>=0.0);
  • Partition needs ownership of the values, so Vec::into_iter is is used.
  • partition() passes a reference to the value to the closure, so &(ref string, value) is used. ref string creates a reference. (_ can also be used).
1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.