SHARE
TWEET

Untitled

a guest Oct 21st, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use std::convert::{AsRef, From};
  2.  
  3. #[derive(Debug)]
  4. pub struct Counter {
  5.     pub name: String,
  6.     pub tally: u64,
  7. }
  8.  
  9. impl Counter {
  10.  
  11.     pub fn new<S>(name: S) -> Counter
  12.     where
  13.       S: AsRef<str> {
  14.         let name = String::from(name.as_ref());
  15.         Counter {
  16.           name,
  17.           tally: 0,
  18.         }
  19.     }
  20.     pub fn max(x: Counter, y: Counter) -> Counter {
  21.         if x.tally > y.tally {
  22.             x
  23.         } else {
  24.             y
  25.         }
  26.     }
  27.  
  28.     pub fn increase(&mut self) {
  29.         self.tally += 1;
  30.     }
  31. }
  32.  
  33. fn main() {
  34.     let mut heads: Counter = Counter::new("heads");
  35.     let mut tails: Counter = Counter::new("tails");
  36.  
  37.     heads.increase();
  38.     heads.increase();
  39.     tails.increase();
  40.  
  41.     if heads.tally == tails.tally {
  42.         println!("Tie");
  43.     } else {
  44.         println!("{:?} wins", Counter::max(heads, tails));
  45.     }
  46. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top