Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::convert::{AsRef, From};
- #[derive(Debug)]
- pub struct Counter {
- pub name: String,
- pub tally: u64,
- }
- impl Counter {
- pub fn new<S>(name: S) -> Counter
- where
- S: AsRef<str> {
- let name = String::from(name.as_ref());
- Counter {
- name,
- tally: 0,
- }
- }
- pub fn max(x: Counter, y: Counter) -> Counter {
- if x.tally > y.tally {
- x
- } else {
- y
- }
- }
- pub fn increase(&mut self) {
- self.tally += 1;
- }
- }
- fn main() {
- let mut heads: Counter = Counter::new("heads");
- let mut tails: Counter = Counter::new("tails");
- heads.increase();
- heads.increase();
- tails.increase();
- if heads.tally == tails.tally {
- println!("Tie");
- } else {
- println!("{:?} wins", Counter::max(heads, tails));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement