Created
April 7, 2017 15:33
-
-
Save U007D/5a61769e279230cea9f5cec2ba6155d0 to your computer and use it in GitHub Desktop.
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(unused_variables)] | |
trait ArgsExtMethods<'a, T> where Self: Iterator<Item = &'a T>, | |
T: Sized + 'a { | |
fn get_item_following_target(&mut self, target: T) -> Option<Self::Item> where Self: Sized, | |
T: PartialEq; | |
} | |
impl<'a, I, T> ArgsExtMethods<'a, T> for I where I: Iterator<Item = &'a T>, | |
T: Sized + PartialEq + 'a { | |
#[inline] | |
fn get_item_following_target(&mut self, target: T) -> Option<Self::Item> where | |
T: PartialEq { | |
self.skip_while(|elem| **elem != target) | |
.skip(1) | |
.next() | |
} | |
} | |
fn main() { | |
let list = ["elem1", "elem2"].to_vec(); | |
let search_param = "elem1"; | |
let expected_result = "elem2"; | |
//get_item_following_target(&search_param) works | |
//but I believe even get_item_following_target({&"elem1"}) should work--neither | |
//ref nor value need to live past the scope of the function call itself | |
let result = list.iter().get_item_following_target("elem1"); | |
//Some(&expected_result) works | |
assert!(result == Some(&expected_result)); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment