Created
September 20, 2019 08:06
-
-
Save rust-play/09c31583add3c242431f083720f2142a to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![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