2022-01-17 12:29:07 +00:00
|
|
|
#![warn(clippy::iter_overeager_cloned, clippy::redundant_clone, clippy::filter_next)]
|
2023-07-02 12:35:19 +00:00
|
|
|
#![allow(dead_code, clippy::let_unit_value, clippy::useless_vec)]
|
2022-01-17 12:29:07 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let vec = vec!["1".to_string(), "2".to_string(), "3".to_string()];
|
|
|
|
|
|
|
|
let _: Option<String> = vec.iter().last().cloned();
|
|
|
|
|
|
|
|
let _: Option<String> = vec.iter().chain(vec.iter()).next().cloned();
|
|
|
|
|
|
|
|
let _: usize = vec.iter().filter(|x| x == &"2").count();
|
|
|
|
|
|
|
|
let _: Vec<_> = vec.iter().take(2).cloned().collect();
|
|
|
|
|
|
|
|
let _: Vec<_> = vec.iter().skip(2).cloned().collect();
|
|
|
|
|
|
|
|
let _ = vec.iter().filter(|x| x == &"2").nth(2).cloned();
|
|
|
|
|
|
|
|
let _ = [Some(Some("str".to_string())), Some(Some("str".to_string()))]
|
2022-06-16 15:39:06 +00:00
|
|
|
.iter()
|
|
|
|
.flatten().cloned();
|
2022-01-17 12:29:07 +00:00
|
|
|
|
2023-08-24 19:32:12 +00:00
|
|
|
let _ = vec.iter().filter(|&x| x.starts_with('2')).cloned();
|
2022-01-17 12:29:07 +00:00
|
|
|
|
2023-08-24 19:32:12 +00:00
|
|
|
let _ = vec.iter().find(|&x| x == "2").cloned();
|
|
|
|
|
|
|
|
{
|
|
|
|
let f = |x: &String| x.starts_with('2');
|
|
|
|
let _ = vec.iter().filter(|&x| f(x)).cloned();
|
|
|
|
let _ = vec.iter().find(|&x| f(x)).cloned();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let vec: Vec<(String, String)> = vec![];
|
|
|
|
let f = move |x: &(String, String)| x.0.starts_with('2');
|
|
|
|
let _ = vec.iter().filter(|&x| f(x)).cloned();
|
|
|
|
let _ = vec.iter().find(|&x| f(x)).cloned();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_move<'a>(
|
|
|
|
iter: impl Iterator<Item = &'a (&'a u32, String)> + 'a,
|
|
|
|
target: String,
|
|
|
|
) -> impl Iterator<Item = (&'a u32, String)> + 'a {
|
|
|
|
iter.filter(move |&(&a, b)| a == 1 && b == &target).cloned()
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct S<'a> {
|
|
|
|
a: &'a u32,
|
|
|
|
b: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bar<'a>(iter: impl Iterator<Item = &'a S<'a>> + 'a, target: String) -> impl Iterator<Item = S<'a>> + 'a {
|
|
|
|
iter.filter(move |&S { a, b }| **a == 1 && b == &target).cloned()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let _ = vec.iter().map(|x| x.len());
|
2022-01-17 12:29:07 +00:00
|
|
|
|
|
|
|
// This would fail if changed.
|
|
|
|
let _ = vec.iter().cloned().map(|x| x + "2");
|
|
|
|
|
2023-08-24 19:32:12 +00:00
|
|
|
let _ = vec.iter().for_each(|x| assert!(!x.is_empty()));
|
2022-01-17 12:29:07 +00:00
|
|
|
|
|
|
|
// Not implemented yet
|
|
|
|
let _ = vec.iter().cloned().all(|x| x.len() == 1);
|
|
|
|
|
|
|
|
// Not implemented yet
|
|
|
|
let _ = vec.iter().cloned().any(|x| x.len() == 1);
|
|
|
|
|
|
|
|
// Should probably stay as it is.
|
|
|
|
let _ = [0, 1, 2, 3, 4].iter().cloned().take(10);
|
2022-06-16 15:39:06 +00:00
|
|
|
|
|
|
|
// `&Range<_>` doesn't implement `IntoIterator`
|
|
|
|
let _ = [0..1, 2..5].iter().cloned().flatten();
|
2022-01-17 12:29:07 +00:00
|
|
|
}
|
2022-04-07 17:39:59 +00:00
|
|
|
|
|
|
|
// #8527
|
|
|
|
fn cloned_flatten(x: Option<&Option<String>>) -> Option<String> {
|
|
|
|
x.cloned().flatten()
|
|
|
|
}
|