Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pub enum SomethingOrNothing<T> {
- Something(T),
- Nothing,
- }
- pub use self::SomethingOrNothing::*;
- pub trait Minimum : Copy {
- fn min(self, b: Self) -> Self;
- }
- pub fn vec_min<T/*: Minimum*/>(v: Vec<T>) -> SomethingOrNothing<T> {
- let mut min = Nothing;
- for e in v {
- min = Something(match min {
- Nothing => e,
- // Here, we can now call the `min` function of the trait.
- Something(n) => {
- e.min(n)
- }
- });
- }
- min
- }
- impl Minimum for i32 {
- fn min(self, b: Self) -> Self {
- if self < b { self } else { b }
- }
- }
- fn main() {
- let v = vec![18,5,7,3,9,27];
- let min = vec_min(v);
- }
Add Comment
Please, Sign In to add comment