.. _mat-rust: 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: .. code-block:: rust use scinumtools3::mat; fn main() -> Result<(), Box> { let water = mat::substance("H2O")?; println!("{}", water.json()?); Ok(()) } Add density and volume inputs for a mixture: .. code-block:: rust use scinumtools3::mat::{self, MatterInput}; fn main() -> Result<(), Box> { 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 0.035 ", "mass_fraction", true, &matter, )?; println!("{}", brine.json()?); Ok(()) } Common workflows ---------------- .. list-table:: :header-rows: 1 * - 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 .. code-block:: rust 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 0.035 ", "mass_fraction", true, &matter, )?; // MatResult let json = brine.json()?; // String See the :doc:`Rust binding guide <../../../integrations/rust>` 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`` 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``.