Skip to main content

ExprResult

Struct ExprResult 

Source
pub struct ExprResult {
    pub total: i32,
    pub rolls: Vec<i32>,
    pub modifier: i32,
}
Expand description

The result of evaluating a dice expression.

This struct contains the final total, all individual die rolls (with their signs), and the sum of all constant modifiers in the expression.

§Fields

  • total: The final result after all rolls and modifiers are applied
  • rolls: All individual die rolls. Rolls from subtracted expressions are negative.
  • modifier: The sum of all constant (non-dice) terms in the expression

§Examples

use dice_parser::DiceExpr;

let expr = DiceExpr::parse("2d6+3").unwrap();
let result = expr.roll().unwrap();

// Access the components of the result
println!("Total: {}", result.total);           // e.g., 10
println!("Dice rolls: {:?}", result.rolls);    // e.g., [3, 4]
println!("Modifier: {}", result.modifier);     // 3

// Total equals sum of rolls plus modifier
assert_eq!(result.total, result.rolls.iter().sum::<i32>() + result.modifier);

§Subtraction Example

use dice_parser::DiceExpr;

let expr = DiceExpr::parse("10 - 2d6").unwrap();
let result = expr.roll().unwrap();

// Subtracted rolls are negative in the rolls vec
assert_eq!(result.modifier, 10);
assert_eq!(result.rolls.len(), 2);
// Both rolls should be negative
assert!(result.rolls[0] < 0 && result.rolls[1] < 0);

Fields§

§total: i32

The total result of the expression.

§rolls: Vec<i32>

All individual die rolls. Subtracted rolls are negative.

§modifier: i32

The sum of all constant modifiers in the expression.

Implementations§

Source§

impl ExprResult

Source

pub fn new(total: i32, rolls: Vec<i32>, modifier: i32) -> Self

Trait Implementations§

Source§

impl Add for ExprResult

Source§

type Output = ExprResult

The resulting type after applying the + operator.
Source§

fn add(self, other: ExprResult) -> Self

Performs the + operation. Read more
Source§

impl Clone for ExprResult

Source§

fn clone(&self) -> ExprResult

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ExprResult

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Neg for ExprResult

Source§

type Output = ExprResult

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self

Performs the unary - operation. Read more
Source§

impl Sub for ExprResult

Source§

type Output = ExprResult

The resulting type after applying the - operator.
Source§

fn sub(self, other: ExprResult) -> Self

Performs the - operation. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V