How to create a tuple from a vector?

不羁的心 提交于 2020-06-27 07:13:33

问题


Here's an example that splits a string and parses each item, putting it into a tuple whose size is known at compile time.

use std::str::FromStr;

fn main() {
    let some_str = "123,321,312";
    let num_pair_str = some_str.split(',').collect::<Vec<&str>>();
    if num_pair_str.len() == 3 {
        let num_pair: (i32, i32, i32) = (
            i32::from_str(num_pair_str[0]).expect("failed to parse number"),
            i32::from_str(num_pair_str[1]).expect("failed to parse number"),
            i32::from_str(num_pair_str[2]).expect("failed to parse number"),
        );
        println!("Tuple {:?}", num_pair);
    }
}

Is there a way to avoid repetition parsing the numbers?

This is an example of what it might look like if Rust supported Python-like comprehensions:

let num_pair: (i32, i32, i32) = (
    i32::from_str(num_pair_str[i]).expect("failed to parse number")
    for i in 0..3
);

Is it possible to declare the tuple in a way that expands the vector?


回答1:


You can't use Python-like list comprehension, as Rust doesn't have it. The closest thing is to do it explicitly via another iterator. You can't directly collect into a tuple, so you need another explicit step to convert the vector:

use std::str::FromStr;

fn main() {
    let some_str = "123,321,312";
    let num_pair_str = some_str.split(',').collect::<Vec<_>>();
    if num_pair_str.len() == 3 {
        let v = num_pair_str.iter().map(|s| i32::from_str(s).expect("failed to parse number"))
            .collect::<Vec<_>>();
        let num_pair: (i32, i32, i32) = (v[0], v[1], v[2]);
        println!("Tuple {:?}", num_pair);
    }
}

If you want to avoid the intermediate vectors you can do something like the following:

use std::str::FromStr;

fn main() {
    let some_str = "123,321,312";
    let it0 = some_str.split(',');
    if it0.clone().count() == 3 {
        let mut it = it0.map(|s| i32::from_str(s).expect("failed to parse number"));
        let num_pair: (i32, i32, i32) =
            (it.next().unwrap(), it.next().unwrap(), it.next().unwrap());
        println!("Tuple {:?}", num_pair);
    }
}



回答2:


You can declare a trait with a method similar to Iterator::collect and implement it to collect to various tuples sizes:

fn main() {
    // Example with some simplifications
    // Note that there is no extra allocation
    let num_pair: (i32, i32, i32) = "123,321,312"
        .split(',')
        .map(|s| s.parse().expect("an i32"))
        .try_collect()
        .expect("a 3-tuple of i32");
    assert_eq!(num_pair, (123, 321, 312));
}

trait TryCollect<T> {
    fn try_collect(&mut self) -> Option<T>;
}

macro_rules! impl_try_collect_tuple {
    () => { };
    ($A:ident $($I:ident)*) => {
        impl_try_collect_tuple!($($I)*);

        impl<$A: Iterator> TryCollect<($A::Item, $($I::Item),*)> for $A {
            fn try_collect(&mut self) -> Option<($A::Item, $($I::Item),*)> {
                let r = (try_opt!(self.next()),
                         // hack: we need to use $I in the expasion
                         $({ let a: $I::Item = try_opt!(self.next()); a}),* );
                Some(r)
            }
        }
    }
}

macro_rules! try_opt {
    ($e:expr) => (match $e { Some(e) => e, None => return None })
}

// implement TryCollect<T> where T is a tuple with size 1, 2, .., 10
impl_try_collect_tuple!(A A A A A A A A A A);

Other examples:

fn main() {
    let mut iter = (0..7).into_iter();

    let (a, b, c) = iter.try_collect().unwrap();
    assert_eq!((a, b, c), (0, 1, 2));

    let (d, e) = iter.try_collect().unwrap();
    assert_eq!((d, e), (3, 4));

    let (f,) = iter.try_collect().unwrap();
    assert_eq!(f, 5);

    let a: Option<(u32, u32)> = iter.try_collect();
    assert_eq!(None, a);
}


来源:https://stackoverflow.com/questions/38863781/how-to-create-a-tuple-from-a-vector

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!