Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::borrow::Borrow;
- use std::iter::FromIterator;
- type Result<T> = std::result::Result<T, Box<std::error::Error>>;
- pub struct Game {
- rolls: Vec<u8>,
- }
- impl Game {
- pub fn new<I>(rolls: I) -> Result<Self> where I: IntoIterator,
- I::Item: Borrow<u8>, {
- Ok(Self {
- rolls: Vec::from_iter(rolls),
- })
- }
- pub fn score(&self) -> u16 {
- self.rolls
- .iter()
- .map(u16::from)
- .sum()
- }
- }
- #[test]
- fn calc_no_rolls_returns_0() -> Result<()> {
- // given an empty `Rolls`
- let game = Game::new(&[])?;
- // when invoked
- let res = game.score();
- // the score should be 0
- assert_eq!(0, res);
- Ok(())
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement