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.

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> 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.