#tuple #iterator #meta

no-std tuples

Provides many useful tools related to tuples

17 stable releases

1.15.0 Nov 22, 2023
1.14.0 Nov 21, 2023
1.12.0 Apr 18, 2023
1.9.0 Mar 20, 2023
1.4.0 Nov 11, 2020

#143 in Rust patterns

Download history 661/week @ 2024-07-24 769/week @ 2024-07-31 639/week @ 2024-08-07 706/week @ 2024-08-14 711/week @ 2024-08-21 684/week @ 2024-08-28 787/week @ 2024-09-04 842/week @ 2024-09-11 585/week @ 2024-09-18 963/week @ 2024-09-25 1564/week @ 2024-10-02 1215/week @ 2024-10-09 1264/week @ 2024-10-16 1106/week @ 2024-10-23 1199/week @ 2024-10-30 989/week @ 2024-11-06

4,706 downloads per month
Used in 5 crates (4 directly)

MIT license

5MB
70K SLoC

tuples

Rust version documentation LICENSE

Provides many useful tools related to tuples

  • Support no-std
  • Support wasm
  • AsRef
  • AsMut
  • AsOption
  • AsResult
  • AsDeref
  • AsDerefMut
  • TupleGet
  • Transpose Option
  • Transpose Result
  • Cloned
  • Copied
  • Flatten
  • Combin
  • Split
  • Mapping
  • Iter
  • IntoIter
  • FromIter
  • Collect
  • Meta Trait
  • Shorthand Macro
  • Call
  • Apply
  • Swap
  • Sort

Examples

  • map

    let a = (1, 2, 3);
    let b = a.map(|v| v * 3);
    assert_eq!(b, (3, 6, 9));
    
  • mapN

    let a = (1, 2, 3, 4, 5);
    let b = a.map3(|v| v * 5);
    assert_eq!(b, (1, 2, 3, 20, 5));
    
  • map_all

    let a = (1, 2, 3);
    let b = a.map_all(|v| v * 10, |v| v * 100, |v| v * 1000);
    assert_eq!(b, (10, 200, 3000));
    
  • as_ref

    let t = (5, 6, 7);
    let (a, b, c) = t.as_ref();
    assert_eq!(*a, 5);
    assert_eq!(*b, 6);
    assert_eq!(*c, 7);
    
  • cloned

    let a = (&1, &2, &3);
    let b = a.cloned();
    assert_eq!(b, (1, 2, 3))
    
  • flatten

    let a = ((1, 2, 3), (4, 5, 6), (7, 8, 9));
    let b = a.flatten();
    assert_eq!(b, (1, 2, 3, 4, 5, 6, 7, 8, 9));
    
  • meta

    let a = (1, 2, 3, 4, 5);
    assert_eq!(a.arity(), 5);
    
    let b = ();
    assert_eq!(b.arity(), 0);
    
  • get

    let a = (1, 2, 3, 4, 5);
    assert_eq!(*a.get(2), 3);
    
    let mut a = (1, 2, 3, 4, 5);
    *a.get_mut(3) = 6;
    
  • iter

    let a = (1, 2, 3)
        .into_iter()
        .map(|v| v * 3)
        .collect_tuple::<tuple![3;]>();
    let b: (i32, i32, i32) = (3, 6, 9);
    assert_eq!(a, b);
    
    let a = (1, 2, 3)
        .into_iter()
        .map(|v| v * 3)
        .try_collect_tuple::<tuple![3;]>();
    let b: Option<(i32, i32, i32)> = Some((3, 6, 9));
    assert_eq!(a, b);
    
    let a = (1, 2, 3)
        .into_iter()
        .map(|v| v * 3)
        .collect_tuple_try::<tuple![3;]>();
    let b: (Option<i32>, Option<i32>, Option<i32>) = (Some(3), Some(6), Some(9));
    assert_eq!(a, b);
    
  • transpose

    let a = Some((1, 2, 3)).transpose();
    assert_eq!(a, (Some(1), Some(2), Some(3)));
    
    let b = (Some(1), Some(2), Some(3)).transpose();
    assert_eq!(b, Some((1, 2, 3)));
    
    let a: (Result<u8, ()>, Result<u8, ()>, Result<u8, ()>) = (Ok(1), Ok(2), Ok(3));
    let b: Result<(u8, u8, u8), ()> = a.transpose();
    assert_eq!(b, Ok((1, 2, 3)));
    
    let a: (Result<u8, i16>, Result<u8, i32>, Result<u8, i64>) = (Ok(1), Err(-1), Ok(3));
    let b: Result<(u8, u8, u8), i64> = a.transpose();
    assert_eq!(b, Err(-1));
    
    let a: (Result<u8, i16>, Result<u8, i32>, Result<u8, i64>) = (Ok(1), Err(-1), Ok(3));
    let b = a.transpose1::<i64>();
    assert_eq!(b, Err(-1));
    
  • combin

    let a = (1, 2).push_right(3);
    assert_eq!(a, (1, 2, 3));
    
    let b = (2, 1).push_left(3);
    assert_eq!(b, (3, 2, 1));
    
    let c = (1, 2, 3).concat((4, 5, 6));
    assert_eq!(c, (1, 2, 3, 4, 5, 6))
    
  • split

    • split_parts
      let t = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
      
      let a = t.split_2_parts();
      assert_eq!(a, ((0, 1, 2, 3, 4), (5, 6, 7, 8, 9)));
      
      let b = t.split_3_parts();
      assert_eq!(b, ((0, 1, 2, 3), (4, 5, 6), (7, 8, 9)));
      
      let c = t.split_4_parts();
      assert_eq!(c, ((0, 1, 2), (3, 4, 5), (6, 7), (8, 9)));
      
      let d = t.split_5_parts();
      assert_eq!(d, ((0, 1), (2, 3), (4, 5), (6, 7), (8, 9)));
      
    • split_at
      let t = (1, 2, 3, 4, 5, 6);
      
      let a = t.split_at_1();
      assert_eq!(a, (1, (2, 3, 4, 5, 6)));
      
      let b = t.split_at_3();
      assert_eq!(b, ((1, 2, 3), (4, 5, 6)));
      
      let c = t.split_at_5();
      assert_eq!(c, ((1, 2, 3, 4, 5), 6));
      
    • split_by
      let t = (1, 2, 3, 4, 5, 6);
      let t2 = (1, 2, 3, 4, 5);
      
      let a = t.split_by_2();
      assert_eq!(a, ((1, 2), (3, 4), (5, 6)));
      
      let b = t2.split_by_2();
      assert_eq!(b, ((1, 2), (3, 4), 5));
      
      let c = t.split_by_3();
      assert_eq!(c, ((1, 2, 3), (4, 5, 6)));
      
      let d = t2.split_by_3();
      assert_eq!(d, ((1, 2, 3), (4, 5)));
      
      let e = t.split_by_6();
      assert_eq!(e, ((1, 2, 3, 4, 5, 6)));
      
    • split_to_tuple_at
      let t = (1, 2, 3, 4, 5, 6);
      
      let a = t.split_to_tuple_at_1();
      assert_eq!(a, ((1,), (2, 3, 4, 5, 6)));
      
      let b = t.split_to_tuple_at_3();
      assert_eq!(b, ((1, 2, 3), (4, 5, 6)));
      
      let c = t.split_to_tuple_at_5();
      assert_eq!(c, ((1, 2, 3, 4, 5), (6,)));
      
    • split_to_tuple_by
      let t = (1, 2, 3, 4, 5, 6);
      let t2 = (1, 2, 3, 4, 5);
      
      let a = t.split_to_tuple_by_2();
      assert_eq!(a, ((1, 2), (3, 4), (5, 6)));
      
      let b = t2.split_to_tuple_by_2();
      assert_eq!(b, ((1, 2), (3, 4), (5,)));
      
      let c = t.split_to_tuple_by_3();
      assert_eq!(c, ((1, 2, 3), (4, 5, 6)));
      
      let d = t2.split_to_tuple_by_3();
      assert_eq!(d, ((1, 2, 3), (4, 5)));
      
      let e = t.split_to_tuple_by_6();
      assert_eq!(e, (((1, 2, 3, 4, 5, 6),)));
      
  • call

    let r = (1, 2, 3).call(|a, b, c| a + b + c);
    assert_eq!(r, 6);
    
  • apply

    let a = (1, 2, 3);
    fn foo(a: i32, b: i32, c: i32) -> i32 {
        a + b + c
    }
    let r = foo.apply_tuple(a);
    assert_eq!(r, 6)
    
  • swap

    let mut a = (1, 2, 3, 4, 5);
    a.swap(1, 3);
    assert_eq!(a, (1, 4, 3, 2, 5));
    
  • swap_n

    Not enabled by default

    features = ["tuple_swap_n"]
    
    let mut a = (1, 2, 3, 4, 5);
    a.swap_1_3();
    assert_eq!(a, (1, 4, 3, 2, 5));
    
  • sort
    currently implemented

    • selection sort

      let mut a = (6, 2, 6, 8, 0, 5);
      a.sort_selection();
      assert_eq!(a, (0, 2, 5, 6, 6, 8))
      

No runtime deps

Features