Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Getting Started

Add the crate:

cargo add ddust

ddust builds on nightly Rust, from nightly-2026-09-28, since its arithmetic is const fn through const traits, which are not stable yet. A rust-toolchain.toml beside the project’s Cargo.toml pins it:

[toolchain]
channel = "nightly-2026-09-28"

It needs no feature, and builds without std or an allocator. Platforms and Features lists what each feature adds.

A First Program

A price read exactly from its text and a quantity written as a literal make a notional, first exactly and then rounded to the cent:

fn a_notional_is_exact_and_rounds_only_when_asked() {
    use ddust::round::HalfEven;
    use ddust::{D64, dec};

    let price: D64<2> = "64250.75".parse().expect("a price to the cent");
    let quantity: D64<4> = dec!(0.0153);

    let notional: D64<6> = (price * quantity).into();
    assert_eq!(notional.to_string(), "983.036475", "exact: two decimals and four make six");

    let billed = price.mul_round(quantity, HalfEven);
    assert_eq!(billed, dec!(983.04), "to the cent, as asked");
}

Each type names its scale: the price has two decimals, the quantity four, and their product six, so the product is exact. Rounding happens only where a mode is named, here half to even: 983.036475 is billed as 983.04. Decimals and Scales shows what a scale is, and Rounding each mode.

Every listing in this book runs as a test:

cargo test -p ddust --all-features --test book

From there, the crate’s page, ddust, lists every type.