Guest User

Untitled

a guest
Oct 15th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. pub enum SomethingOrNothing<T> {
  2. Something(T),
  3. Nothing,
  4. }
  5. pub use self::SomethingOrNothing::*;
  6.  
  7. pub trait Minimum : Copy {
  8. fn min(self, b: Self) -> Self;
  9. }
  10.  
  11. pub fn vec_min<T/*: Minimum*/>(v: Vec<T>) -> SomethingOrNothing<T> {
  12. let mut min = Nothing;
  13. for e in v {
  14. min = Something(match min {
  15. Nothing => e,
  16. // Here, we can now call the `min` function of the trait.
  17. Something(n) => {
  18. e.min(n)
  19. }
  20. });
  21. }
  22. min
  23. }
  24.  
  25. impl Minimum for i32 {
  26. fn min(self, b: Self) -> Self {
  27. if self < b { self } else { b }
  28. }
  29. }
  30.  
  31. fn main() {
  32. let v = vec![18,5,7,3,9,27];
  33. let min = vec_min(v);
  34. }
Add Comment
Please, Sign In to add comment