Less failure, better comments
This commit is contained in:
parent
a111ea482b
commit
1c185c22a8
18
src/roll.rs
18
src/roll.rs
@ -1,20 +1,19 @@
|
||||
//! A struct in which our data is held for rolling. By ensuring that
|
||||
//! only `usize`s are used we prevent the need for additional type
|
||||
//! checking at time of use. */
|
||||
//! checking at time of use.
|
||||
extern crate rand;
|
||||
use rand::Rng;
|
||||
use std::fmt;
|
||||
use std::num::ParseIntError;
|
||||
use super::std;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct Roll {
|
||||
die: usize,
|
||||
number: usize,
|
||||
}
|
||||
impl Roll {
|
||||
/// A fn to create new `Roll` structs, private to prevent
|
||||
/// being used outside the library.
|
||||
/// A function to create new `Roll` structs.
|
||||
pub fn new(number: usize, die: usize) -> Roll {
|
||||
Roll {
|
||||
die: die,
|
||||
@ -26,7 +25,7 @@ impl Roll {
|
||||
///Allows the fields of `Roll` to be private (and unchangable)
|
||||
///while still allowing access to act on their internal value
|
||||
/// ```
|
||||
/// let x = Roll::new(&2, &6);
|
||||
/// let x = Roll::new(2, 6);
|
||||
/// assert!(x.number() == 2);
|
||||
/// ```
|
||||
pub fn number(&self) -> usize {
|
||||
@ -41,14 +40,14 @@ impl Roll {
|
||||
/// Creates a Vector to hold each of the values rolled. Making
|
||||
/// a Vector ensures the ability to work on exactly the same
|
||||
/// die rolls easily and accurately, since calling the `Roll`
|
||||
/// iterator again will produce new results.
|
||||
/// iterator again will produce new results. Consumes the `Roll`
|
||||
pub fn rolls(self) -> Vec<usize> {
|
||||
self.collect()
|
||||
}
|
||||
|
||||
/// Used on a `Roll` to return only the final value, ignoring
|
||||
/// the values used to add up to that result. Allows `.total()`
|
||||
/// syntax when used
|
||||
/// syntax when used. Consumes the `Roll`.
|
||||
pub fn total(self) -> usize {
|
||||
self.sum()
|
||||
}
|
||||
@ -123,8 +122,9 @@ fn displays() {
|
||||
#[test]
|
||||
fn rolls() {
|
||||
let x = Roll::new(20, 6);
|
||||
for y in x.clone().rolls() {
|
||||
assert!(y <= x.die());
|
||||
let y = x.die;
|
||||
for z in x.rolls() {
|
||||
assert!(z <= y);
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
|
Loading…
x
Reference in New Issue
Block a user