Skip to content

API Reference

recipe-ratio exposes four free functions. All take and return f64 (grams, or any consistent weight unit). No structs, no traits, no features to enable. Every function is division-safe: a non-positive flour (or from_flour) denominator returns 0.0, never a panic.

baker_percent

pub fn baker_percent(ingredient_grams: f64, flour_grams: f64) -> f64

Baker's percentage of an ingredient relative to flour weight, with flour fixed at 100%. Returns ingredient / flour × 100.

  • Returns 0.0 when flour_grams <= 0.0 (no division by zero).
  • Flour itself evaluates to 100.0.
use recipe_ratio::baker_percent;
assert!((baker_percent(300.0, 500.0) - 60.0).abs() < 1e-9);   // 60% water
assert!((baker_percent(500.0, 500.0) - 100.0).abs() < 1e-9);  // flour is 100%
assert!((baker_percent(10.0, 500.0) - 2.0).abs() < 1e-9);     // 2% salt
assert_eq!(baker_percent(300.0, 0.0), 0.0);                   // safe on zero flour

hydration

pub fn hydration(water_grams: f64, flour_grams: f64) -> f64

Dough hydration — the baker's percentage of water, i.e. water / flour × 100. This is the single number bakers use to describe how wet a dough is. Thin: 55–60% (bagels, sandwich bread). Medium: 60–68% (lean breads). High: 70–85% (ciabatta, focaccia).

Equivalent to baker_percent(water_grams, flour_grams); provided as a named alias because hydration is the ratio bakers reach for first.

use recipe_ratio::hydration;
assert!((hydration(300.0, 500.0) - 60.0).abs() < 1e-9);  // a 60% dough
assert!((hydration(400.0, 500.0) - 80.0).abs() < 1e-9);  // a wet 80% dough

scale

pub fn scale(ingredient_grams: f64, from_flour: f64, to_flour: f64) -> f64

Scale an ingredient weight when the flour in a recipe changes from from_flour to to_flour, holding the ratio constant.

result = ingredient × (to_flour / from_flour)

  • Returns 0.0 when from_flour <= 0.0.
  • Doubling the flour doubles every scaled ingredient; halving the flour halves them.
use recipe_ratio::scale;
// 300 g water at 500 g flour → scaled to a 1 kg flour batch
assert!((scale(300.0, 500.0, 1000.0) - 600.0).abs() < 1e-9);  // doubled
assert!((scale(300.0, 500.0, 250.0)  - 150.0).abs() < 1e-9);  // halved
assert_eq!(scale(300.0, 0.0, 1000.0), 0.0);                    // safe

dough_weight

pub fn dough_weight(ingredient_grams: &[f64]) -> f64

Total dough weight — the sum of every ingredient weight (flour + water + salt + …). Pass any slice of gram amounts.

use recipe_ratio::dough_weight;
assert!((dough_weight(&[500.0, 300.0, 10.0]) - 810.0).abs() < 1e-9);
assert_eq!(dough_weight(&[]), 0.0);