10 个版本
0.4.1 | 2023年11月28日 |
---|---|
0.4.0 | 2023年11月27日 |
0.3.0 | 2020年6月25日 |
0.2.5 | 2020年2月14日 |
0.1.0 | 2020年1月30日 |
#286 in Rust 模式
每月 33 次下载
96KB
3.5K SLoC
TupleTools
提供实用函数
- fst
- snd
- trd
- pop_back
- pop_front
- append
- prepend
- div
- add
- sub
- mul
- head_tail
与 #[no_std] ! 兼容
示例
#[test]
fn fst_test() {
let x = vec![(2, 3), (3, 4), (4, 5), (5, 6)];
let expected = vec![2, 3, 4, 5];
assert_eq!(
x.iter().map(Fst::fst).cloned().collect::<Vec<_>>(),
expected
);
assert_eq!(
x.clone().into_iter().map(Fst::fst).collect::<Vec<_>>(),
expected
);
assert_eq!(x.into_iter().map(fst).collect::<Vec<_>>(), expected)
}
#[test]
fn snd_test() {
let x = vec![(2, 3), (3, 4), (4, 5), (5, 6)];
let expected = vec![3, 4, 5, 6];
assert_eq!(
x.iter().map(Snd::snd).cloned().collect::<Vec<_>>(),
expected
);
assert_eq!(
x.clone().into_iter().map(Snd::snd).collect::<Vec<_>>(),
expected
);
assert_eq!(x.iter().map(snd).cloned().collect::<Vec<_>>(), expected);
assert_eq!(x.into_iter().map(snd).collect::<Vec<_>>(), expected);
}
#[test]
fn trd_test() {
let x = vec![(2, 3, 4), (3, 4, 5), (4, 5, 6), (5, 6, 7)];
let expected = vec![4, 5, 6, 7];
assert_eq!(
x.iter().map(Trd::trd).cloned().collect::<Vec<_>>(),
expected
);
assert_eq!(
x.clone().into_iter().map(Trd::trd).collect::<Vec<_>>(),
expected
);
assert_eq!(x.iter().map(trd).cloned().collect::<Vec<_>>(), expected);
assert_eq!(x.into_iter().map(trd).collect::<Vec<_>>(), expected);
}
#[test]
fn add() {
let x = vec![(1, 2, 1), (1, 1, 1)];
let y = (1, 1, 1);
let z = (1, 1, 1);
assert_eq!(y.add(z), (2, 2, 2));
assert_eq!(x.into_iter().fold((0, 0, 0), |a, b| a.add(b)), (2, 3, 2));
}
#[test]
fn mul() {
let x = vec![(1, 2, 1), (1, 1, 1)];
let y = (1, 1, 1);
let z = (1, 1, 1);
assert_eq!(y.mul(z), (1, 1, 1));
assert_eq!(x.into_iter().fold((0, 0, 0), |a, b| a.mul(b)), (0, 0, 0));
}
#[test]
fn sub() {
let x = vec![(1, 2, 1), (1, 1, 1)];
let y = (1, 1, 1);
let z = (1, 1, 1);
assert_eq!(y.sub(z), (0, 0, 0));
assert_eq!(x.into_iter().fold((0, 0, 0), |a, b| a.sub(b)), (-2, -3, -2));
}
#[test]
fn div() {
let x = vec![(1, 2, 1), (1, 1, 1)];
let y = (1, 1, 1);
let z = (1, 1, 1);
assert_eq!(y.div(z), (1, 1, 1));
assert_eq!(
x.into_iter().fold((1, 1, 1), |a, b| a.div(b)),
(1, 1 / 2, 1)
);
}
#[test]
fn cast_one_to_one() {
let x: (u32, u32, u32, u32, u32, u32) = (3, 4, 5, 6, 7, 8);
let _z: (u64, u64, u64, u64, u64, u64) = x.cast();
}
#[test]
fn append() {
let x = (1, 3, 4, 5);
let y = x.append(5);
assert_eq!((1, 3, 4, 5, 5), y);
}
#[test]
fn prepend() {
let x = (1, 3, 4, 5);
assert_eq!((5, 1, 3, 4, 5), x.prepend(5))
}
#[test]
fn pop_back() {
let x = (1, 3, 4, 5);
assert_eq!((1, 3, 4), x.pop_back())
}
#[test]
fn pop_front() {
let x = (1, 3, 4, 5);
assert_eq!((3, 4, 5), x.pop_front())
}
#[test]
fn head_tail() {
let x = (1,3,4,5);
assert_eq!((1,(3,4,5)),x.head_tail())
}