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 appliedrolls: 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: i32The total result of the expression.
rolls: Vec<i32>All individual die rolls. Subtracted rolls are negative.
modifier: i32The sum of all constant modifiers in the expression.
Implementations§
Trait Implementations§
Source§impl Add for ExprResult
impl Add for ExprResult
Source§type Output = ExprResult
type Output = ExprResult
The resulting type after applying the
+ operator.Source§fn add(self, other: ExprResult) -> Self
fn add(self, other: ExprResult) -> Self
Performs the
+ operation. Read moreSource§impl Clone for ExprResult
impl Clone for ExprResult
Source§fn clone(&self) -> ExprResult
fn clone(&self) -> ExprResult
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for ExprResult
impl Debug for ExprResult
Source§impl Neg for ExprResult
impl Neg for ExprResult
Source§impl Sub for ExprResult
impl Sub for ExprResult
Source§type Output = ExprResult
type Output = ExprResult
The resulting type after applying the
- operator.Source§fn sub(self, other: ExprResult) -> Self
fn sub(self, other: ExprResult) -> Self
Performs the
- operation. Read moreAuto Trait Implementations§
impl Freeze for ExprResult
impl RefUnwindSafe for ExprResult
impl Send for ExprResult
impl Sync for ExprResult
impl Unpin for ExprResult
impl UnwindSafe for ExprResult
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more