diff --git a/Examples.md b/Examples.md index 74a55f4..554f0e5 100644 --- a/Examples.md +++ b/Examples.md @@ -1134,3 +1134,133 @@ fn main ``` + +There doesn't seem to be anything else syntactically relevant in the other subsubsections here, until we get to... + +##### Examples in std + +**Iterator::any** + + + + + + + +
RustHusht
+ +``` +pub trait Iterator { + type Item; + + fn any(&mut self, f: F) -> bool where + F: FnMut(Self::Item) -> bool; +} + +fn main() { + let vec1 = vec![1, 2, 3]; + let vec2 = vec![4, 5, 6]; + + println!("2 in vec1: {}", vec1.iter() .any(|&x| x == 2)); + println!("2 in vec2: {}", vec2.into_iter().any(|x| x == 2)); + + println!("vec1 len: {}", vec1.len()); + println!("First element of vec1 is: {}", vec1[0]); + // `into_iter()` does move `vec2` and its elements, so they cannot be used again + + let array1 = [1, 2, 3]; + let array2 = [4, 5, 6]; + + println!("2 in array1: {}", array1.iter() .any(|&x| x == 2)); + println!("2 in array2: {}", array2.into_iter().any(|x| x == 2)); +} +``` + + +``` +pub trait Iterator + type Item + + fn any(&mut self, f: F) -> bool where + F: FnMut(Self::Item) -> bool //! Should we be able to drop the `Self::` here? +} + +fn main + let vec1 = vec![1, 2, 3] + vec2 = vec![4, 5, 6] + + println! "2 in vec1: {}", vec1.iter() .any(|&x| x == 2)); + println!("2 in vec2: {}", vec2.into_iter().any(|x| x == 2)); + + println!("vec1 len: {}", vec1.len()); + println!("First element of vec1 is: {}", vec1[0]); + // `into_iter()` does move `vec2` and its elements, so they cannot be used again + + let array1 = [1, 2, 3]; + let array2 = [4, 5, 6]; + + println!("2 in array1: {}", array1.iter() .any(|&x| x == 2)); + println!("2 in array2: {}", array2.into_iter().any(|x| x == 2)); +} +``` +
+ +**Searching through iterators** + + + + + + + +
RustHusht
+ +``` + +``` + + +``` + +``` +
+ +#### Higher order functions + + + + + + + +
RustHusht
+ +``` + +``` + + +``` + +``` +
+ +#### Diverging functions + + + + + + + +
RustHusht
+ +``` + +``` + + +``` + +``` +