Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created September 20, 2019 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rust-play/09c31583add3c242431f083720f2142a to your computer and use it in GitHub Desktop.
Save rust-play/09c31583add3c242431f083720f2142a to your computer and use it in GitHub Desktop.
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