Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code shared from the Rust Playground
#![allow(dead_code)]
#![allow(unused_variables)]
mod real_impl {
pub fn find<El>(haystack: impl IntoIterator<Item = El>, needle: El) -> Option<usize>
where
El: core::cmp::PartialEq,
{
haystack
.into_iter()
.enumerate()
.find(|(_, item)| item == &needle)
.map(|(index, _)| index)
}
}
mod naive {
fn foo(x: &[i32], y: i32) -> Option<usize> {
// 10000 строк нечитаемого кода
super::real_impl::find(x, &y)
}
#[test]
fn test() {
assert_eq!(foo(&[1, 2, 3], 2), Some(1));
assert_eq!(foo(&[1, 2, 3], 4), None);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment