Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. use std::borrow::Borrow;
  2. use std::iter::FromIterator;
  3. type Result<T> = std::result::Result<T, Box<std::error::Error>>;
  4.  
  5. pub struct Game {
  6. rolls: Vec<u8>,
  7. }
  8.  
  9. impl Game {
  10. pub fn new<I>(rolls: I) -> Result<Self> where I: IntoIterator,
  11. I::Item: Borrow<u8>, {
  12. Ok(Self {
  13. rolls: Vec::from_iter(rolls),
  14. })
  15. }
  16.  
  17. pub fn score(&self) -> u16 {
  18. self.rolls
  19. .iter()
  20. .map(u16::from)
  21. .sum()
  22. }
  23. }
  24.  
  25. #[test]
  26. fn calc_no_rolls_returns_0() -> Result<()> {
  27. // given an empty `Rolls`
  28. let game = Game::new(&[])?;
  29.  
  30. // when invoked
  31. let res = game.score();
  32.  
  33. // the score should be 0
  34. assert_eq!(0, res);
  35. Ok(())
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement