Rust

The mat module returns canonical JSON records for dependency-free access to every typed field.

Basic examples

Evaluate a formula using the scinumtools3 crate:

use scinumtools3::mat;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let water = mat::substance("H2O")?;
    println!("{}", water.json()?);
    Ok(())
}

Add density and volume inputs for a mixture:

use scinumtools3::mat::{self, MatterInput};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let matter = MatterInput {
        mass_density: Some("1.02*g/cm3".into()),
        volume: Some("1*L".into()),
        ..Default::default()
    };
    let brine = mat::material_with(
        "0.965 <H2O> 0.035 <NaCl>", "mass_fraction", true, &matter,
    )?;
    println!("{}", brine.json()?);
    Ok(())
}

Common workflows

Task

Call chain

Result

Evaluate a substance

mat::substance; MatResult::json

MatResult; String

Add matter inputs

MatterInput; mat::substance_with

MatResult

Normalize a mixture

mat::material_with(expression, normalization, natural, &matter)

MatResult

Read individual fields

Parse the JSON returned by json()

Application-defined data structure

Handle failure

Propagate the crate Result

Err containing the native message

use scinumtools3::mat::{self, MatterInput};

let matter = MatterInput {
    mass_density: Some("1.02*g/cm3".into()),
    volume: Some("1*L".into()),
    ..Default::default()
};
let brine = mat::material_with(
    "0.965 <H2O> 0.035 <NaCl>", "mass_fraction", true, &matter,
)?;                                                   // MatResult
let json = brine.json()?;                             // String

See the Rust binding guide for isotope and matter options.

Current limitations and differences

Compared with the canonical C++ implementation:

  • The safe wrapper owns a native result and frees it on drop, but MatResult::json() returns a String rather than typed Rust composition and matter structures. Parse that JSON to access individual fields.

  • Matter inputs and normalization modes are strings. There are no mutable native composition objects, incremental add() calls, or configurable command objects; each evaluation receives all its inputs at once.

  • Errors use Result. Isotope selection, normalization, and physical assumptions are inherited from C++.

Binding reference

This is a lookup index, not a sequence to read from top to bottom. Use Common workflows above to choose a task, then find the exact Rust name below. Result<T> means the call can fail. The Rust API returns canonical JSON rather than generated MAT records, so field access happens after parsing that JSON.

  • mat::element(expression) and mat::element_with(expression, natural, &matter).

  • mat::substance(expression) and mat::substance_with(expression, natural, &matter).

  • mat::material(expression) and mat::material_with(expression, normalization, natural, &matter).

  • mat::MatterInput string fields number_density, mass_density, volume, and mass.

  • MatResult::json() returns the complete result record as UTF-8 JSON.

Normalization names are number, number_fraction, and mass_fraction.