4 fn test_intersperse() {
5 let v = std::iter::empty().intersperse(0u32).collect::<Vec<_>>();
8 let v = std::iter::once(1).intersperse(0).collect::<Vec<_>>();
9 assert_eq!(v, vec![1]);
11 let xs = ["a", "", "b", "c"];
12 let v: Vec<&str> = xs.iter().map(|x| *x).intersperse(", ").collect();
13 let text: String = v.concat();
14 assert_eq!(text, "a, , b, c".to_string());
16 let ys = [0, 1, 2, 3];
17 let mut it = ys[..0].iter().map(|x| *x).intersperse(1);
18 assert!(it.next() == None);
22 fn test_intersperse_size_hint() {
23 let iter = std::iter::empty::<i32>().intersperse(0);
24 assert_eq!(iter.size_hint(), (0, Some(0)));
26 let xs = ["a", "", "b", "c"];
27 let mut iter = xs.iter().map(|x| *x).intersperse(", ");
28 assert_eq!(iter.size_hint(), (7, Some(7)));
30 assert_eq!(iter.next(), Some("a"));
31 assert_eq!(iter.size_hint(), (6, Some(6)));
32 assert_eq!(iter.next(), Some(", "));
33 assert_eq!(iter.size_hint(), (5, Some(5)));
35 assert_eq!([].iter().intersperse(&()).size_hint(), (0, Some(0)));
39 fn test_fold_specialization_intersperse() {
40 let mut iter = (1..2).intersperse(0);
41 iter.clone().for_each(|x| assert_eq!(Some(x), iter.next()));
43 let mut iter = (1..3).intersperse(0);
44 iter.clone().for_each(|x| assert_eq!(Some(x), iter.next()));
46 let mut iter = (1..4).intersperse(0);
47 iter.clone().for_each(|x| assert_eq!(Some(x), iter.next()));
51 fn test_try_fold_specialization_intersperse_ok() {
52 let mut iter = (1..2).intersperse(0);
53 iter.clone().try_for_each(|x| {
54 assert_eq!(Some(x), iter.next());
58 let mut iter = (1..3).intersperse(0);
59 iter.clone().try_for_each(|x| {
60 assert_eq!(Some(x), iter.next());
64 let mut iter = (1..4).intersperse(0);
65 iter.clone().try_for_each(|x| {
66 assert_eq!(Some(x), iter.next());
72 fn test_intersperse_with() {
73 #[derive(PartialEq, Debug)]
77 let r = [NotClone { u: 0 }, NotClone { u: 1 }]
79 .intersperse_with(|| NotClone { u: 2 })
81 assert_eq!(r, vec![NotClone { u: 0 }, NotClone { u: 2 }, NotClone { u: 1 }]);
88 let r = (0..3).intersperse_with(separator).collect::<Vec<_>>();
89 assert_eq!(r, vec![0, 200, 1, 400, 2]);
93 fn test_intersperse_fold() {
94 let v = (1..4).intersperse(9).fold(Vec::new(), |mut acc, x| {
98 assert_eq!(v.as_slice(), [1, 9, 2, 9, 3]);
100 let mut iter = (1..4).intersperse(9);
101 assert_eq!(iter.next(), Some(1));
102 let v = iter.fold(Vec::new(), |mut acc, x| {
106 assert_eq!(v.as_slice(), [9, 2, 9, 3]);
108 struct NoneAtStart(i32); // Produces: None, Some(2), Some(3), None, ...
109 impl Iterator for NoneAtStart {
111 fn next(&mut self) -> Option<i32> {
113 Some(self.0).filter(|i| i % 3 != 1)
117 let v = NoneAtStart(0).intersperse(1000).fold(0, |a, b| a + b);
122 fn test_intersperse_collect_string() {
123 let contents = [1, 2, 3];
125 let contents_string = contents
127 .map(|id| id.to_string())
128 .intersperse(", ".to_owned())
129 .collect::<String>();
130 assert_eq!(contents_string, "1, 2, 3");
134 fn test_try_fold_specialization_intersperse_err() {
135 let orig_iter = ["a", "b"].iter().copied().intersperse("-");
137 // Abort after the first item.
138 let mut iter = orig_iter.clone();
139 iter.try_for_each(|_| None::<()>);
140 assert_eq!(iter.next(), Some("-"));
141 assert_eq!(iter.next(), Some("b"));
142 assert_eq!(iter.next(), None);
144 // Abort after the second item.
145 let mut iter = orig_iter.clone();
146 iter.try_for_each(|item| if item == "-" { None } else { Some(()) });
147 assert_eq!(iter.next(), Some("b"));
148 assert_eq!(iter.next(), None);
150 // Abort after the third item.
151 let mut iter = orig_iter.clone();
152 iter.try_for_each(|item| if item == "b" { None } else { Some(()) });
153 assert_eq!(iter.next(), None);