Struct rouler::Roller [] [src]

pub struct Roller<'a> { /* fields omitted */ }

The Roller is the core struct of the library. The basic principle is to provide a reusable container that provides a specific kind of die roll, so that it can be quickly and easily repeated whenever called for. Each container contains the syntax of the roll type it represents, and the value of the last roll it made. Containers are thus self-mutating, but self-contained.

The main benefit is thus in code organization, as one can store different types of rolls for easy use later.

Methods

impl<'a> Roller<'a>
[src]

Creates a new Roller with the given die roll syntax, and populates the stored total with a first roll of the indicated dice. Because subsequent rerolls mutate the object in order to store the most recent roll, it should be declared with let mut if you intend to reroll it.

Panics

As a Roller rolls itself on creation, it thus triggers the parser, and incorrect syntax will cause a panic.

Examples

use rouler::Roller;

let mut laser_damage = Roller::new("1d4*10");

println!("Damage, rnd 1: {}", laser_damage.total());
println!("Damage, rnd 2: {}", laser_damage.reroll());

Rollers implement Eq and Ord, based on their current totals, so die results can be compared directly without having to first call the total method:

let att = Roller::new("1d20 + 5");
let def = Roller::new("1d20 + 2");
 
if att > def {
    println!("You struck the monster!");        
} else {
    println!("You missed!");
}

For convenience's sake, Rollers also implement Display, so they are printable:

println!("{}", Roller::new("4d8 + 5"));
// => [4d8 + 5: 24]

Rolls the Roller's die roll, stores the value in total, and then returns it. In this way, you can use Roller::reroll() in place as a value, rather than needing to call the method seperately.

Examples

use rouler::Roller;

let mut stat = Roller::new("3d6");

println!("STR (3d6): {}", stat.total());        // => STR (3d6): 14
println!("DEX (3d6+1): {}", stat.reroll() + 1); // => DEX (3d6+1): 13

println!("{}", stat);                           // prints [3d6: 12]

Returns the result of the last roll made by the Roller.

Returns a reference to self for use as an Iterator. This allows for iterating infinitely and lazily over successive rolls of the dice. By borrowing as mutable, the state of the internal total is preserved, so that calls to total() will remain consistent.

Examples

// Collect multiple results to a vector:
let stats = Roller::new("3d6").iter().take(6).collect::<Vec<i64>>();

Remember! Rollers are infinite iterators: always use take() to avoid infinite loops! This is safe:

// Keep rolling until a result greater than a threshold:
assert!(Roller::new("4d6").iter().skip_while(|&x| x < 13).take(1).last().unwrap() >= 13);

But this will not terminate:

assert!(Roller::new("4d6").iter().skip_while(|&x| x < 13).last().unwrap() >= 13);

Trait Implementations

impl<'a> Debug for Roller<'a>
[src]

Formats the value using the given formatter.

impl<'a> Clone for Roller<'a>
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl<'a> Copy for Roller<'a>
[src]

impl<'a> Iterator for Roller<'a>
[src]

The type of the elements being iterated over.

Advances the iterator and returns the next value. Read more

Returns the bounds on the remaining length of the iterator. Read more

Consumes the iterator, counting the number of iterations and returning it. Read more

Consumes the iterator, returning the last element. Read more

Returns the nth element of the iterator. Read more

🔬 This is a nightly-only experimental API. (iterator_step_by)

unstable replacement of Range::step_by

Creates an iterator starting at the same point, but stepping by the given amount at each iteration. Read more

Takes two iterators and creates a new iterator over both in sequence. Read more

'Zips up' two iterators into a single iterator of pairs. Read more

Takes a closure and creates an iterator which calls that closure on each element. Read more

Creates an iterator which uses a closure to determine if an element should be yielded. Read more

Creates an iterator that both filters and maps. Read more

Creates an iterator which gives the current iteration count as well as the next value. Read more

Creates an iterator which can use peek to look at the next element of the iterator without consuming it. Read more

Creates an iterator that [skip]s elements based on a predicate. Read more

Creates an iterator that yields elements based on a predicate. Read more

Creates an iterator that skips the first n elements. Read more

Creates an iterator that yields its first n elements. Read more

An iterator adaptor similar to [fold] that holds internal state and produces a new iterator. Read more

Creates an iterator that works like map, but flattens nested structure. Read more

Creates an iterator which ends after the first [None]. Read more

Do something with each element of an iterator, passing the value on. Read more

Borrows an iterator, rather than consuming it. Read more

Transforms an iterator into a collection. Read more

Consumes an iterator, creating two collections from it. Read more

An iterator adaptor that applies a function, producing a single, final value. Read more

Tests if every element of the iterator matches a predicate. Read more

Tests if any element of the iterator matches a predicate. Read more

Searches for an element of an iterator that satisfies a predicate. Read more

Searches for an element in an iterator, returning its index. Read more

Searches for an element in an iterator from the right, returning its index. Read more

Returns the maximum element of an iterator. Read more

Returns the minimum element of an iterator. Read more

Returns the element that gives the maximum value from the specified function. Read more

Returns the element that gives the maximum value with respect to the specified comparison function. Read more

Returns the element that gives the minimum value from the specified function. Read more

Returns the element that gives the minimum value with respect to the specified comparison function. Read more

Reverses an iterator's direction. Read more

Converts an iterator of pairs into a pair of containers. Read more

Creates an iterator which [clone]s all of its elements. Read more

Repeats an iterator endlessly. Read more

Sums the elements of an iterator. Read more

Iterates over the entire iterator, multiplying all the elements Read more

Lexicographically compares the elements of this Iterator with those of another. Read more

Lexicographically compares the elements of this Iterator with those of another. Read more

Determines if the elements of this Iterator are equal to those of another. Read more

Determines if the elements of this Iterator are unequal to those of another. Read more

Determines if the elements of this Iterator are lexicographically less than those of another. Read more

Determines if the elements of this Iterator are lexicographically less or equal to those of another. Read more

Determines if the elements of this Iterator are lexicographically greater than those of another. Read more

Determines if the elements of this Iterator are lexicographically greater than or equal to those of another. Read more

impl<'a> PartialEq for Roller<'a>
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl<'a> Eq for Roller<'a>
[src]

impl<'a> Ord for Roller<'a>
[src]

This method returns an Ordering between self and other. Read more

impl<'a> PartialOrd for Roller<'a>
[src]

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl<'a> Display for Roller<'a>
[src]

Formats the value using the given formatter. Read more