Expand description
Safe, idiomatic Rust bindings to the IUPAC InChI reference library for generating InChI and InChIKey chemical identifiers.
The InChI C library is vendored and statically linked through the
inchi-sys crate, so there is nothing to install and no
network access at build time. This crate wraps it in a panic-free,
Result-based API where every native allocation is released
automatically (RAII) and no unsafe is exposed at the public boundary.
§Quickstart
Generate an InChI and InChIKey from a Molfile. Pass () for default
options (or Options to customize):
use inchi::{from_molfile, inchikey};
// A minimal V2000 Molfile for methane (one carbon; hydrogens are implicit).
let methane = "\n example\n\n 1 0 0 0 0 0 0 0 0 0999 V2000\n\
\x20 0.0000 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\nM END\n";
let result = from_molfile(methane, ())?;
assert_eq!(result.inchi(), "InChI=1S/CH4/h1H4");
let key = inchikey(result.inchi())?;
assert_eq!(key, "VNWKTOKETHGBQD-UHFFFAOYSA-N");Or build a structure programmatically:
use inchi::{Molecule, Atom, BondOrder};
let mut ethanol = Molecule::new();
let c1 = ethanol.add_atom(Atom::new("C"));
let c2 = ethanol.add_atom(Atom::new("C"));
let o = ethanol.add_atom(Atom::new("O"));
ethanol.add_bond(c1, c2, BondOrder::Single)?;
ethanol.add_bond(c2, o, BondOrder::Single)?;
assert_eq!(ethanol.to_inchi(())?.inchi(), "InChI=1S/C2H6O/c1-2-3/h3H,2H2,1H3");§API overview
This crate wraps the complete InChI generation, parsing, and validation API.
For concepts and worked examples (standard vs. non-standard InChI, stereo
conventions, polymers), see the guide.
Structure → InChI
from_molfile— Molfile/SDF text (MakeINCHIFromMolfileText).Molecule::to_inchi— a programmatically built structure (GetINCHI, orGetINCHIExwhen polymer units are present).
InChI → structure
struct_from_inchi— atoms, bonds, and 0D stereo (GetStructFromINCHI).struct_from_inchi_ex— the above plus polymer data (GetStructFromINCHIEx).struct_from_aux_info— recover a structure from anAuxInfoblock.
InChI → InChIKey (feature key)
inchikey/inchikey_with_hashes(GetINCHIKeyFromINCHI).
Conversion & validation
inchi_to_inchi— re-derive/normalize an InChI (GetINCHIfromINCHI).check_inchi— validate an InChI string (CheckINCHI).check_inchikey— validate an InChIKey string (CheckINCHIKey).
Generation is tuned through Options, including the experimental
Polymers extension. Native allocations are released automatically.
§Feature flags
| Feature | Default | Description |
|---|---|---|
key | yes | Enables inchikey/inchikey_with_hashes (GetINCHIKeyFromINCHI). |
regenerate-bindings | no | Regenerates the FFI bindings with bindgen (needs libclang). |
§Thread safety
The underlying InChI C library keeps internal static state and is not
guaranteed to be re-entrant, so every call into it is serialized behind a
global lock. All functions in this crate are therefore safe to call from
multiple threads, but calls do not run concurrently with one another. The
public types are Send and Sync.
§Minimum supported Rust version
This crate supports Rust 1.77 and later. Raising the MSRV is treated as a semver-breaking change.
Modules§
- guide
- User guide — concepts and worked examples.
Structs§
- Atom
- A single atom in a
Molecule. - Extended
Structure - A
Structuretogether with any polymer data recovered from an InChI bystruct_from_inchi_ex. - Inchi
Key key - An InChIKey together with the optional 256-bit hash extensions.
- Inchi
Output - The successful result of generating an InChI.
- Molecule
- A molecular structure assembled programmatically from atoms, bonds, and 0D stereo descriptors.
- Options
- A builder for InChI generation options.
- Polymer
Unit - A single polymer structural repeating unit (CTFile Sgroup).
- Structure
- A molecular structure recovered from an InChI string.
- Structure
Atom - One atom of a
Structurerecovered from an InChI. - Structure
Bond - One bond of a
Structure, referencing atoms by index.
Enums§
- Bond
Order - The order of a covalent bond.
- ImplicitH
- How many implicit hydrogens an atom carries.
- Inchi
Error - The error type returned by all fallible operations in this crate.
- Inchi
KeyValidity - The verdict returned by
check_inchikeyfor an InChIKey string. - Inchi
Validity - The verdict returned by
check_inchifor an InChI string. - Parity
- A 0D stereo parity (used when no coordinates disambiguate the geometry).
- Polymer
Connection - The CTFile
SCNconnection scheme of aPolymerUnit. - Polymer
Subtype - The CTFile
SSTunit subtype of aPolymerUnit. - Polymer
Unit Kind - The CTFile
STYunit type of aPolymerUnit. - Polymers
- How polymer structural-repeating-unit (SRU) data is processed.
- Radical
- The unpaired-electron (radical) state of an atom.
- Status
- The status class returned by the InChI C library.
- Stereo
- A single 0D stereo element, referencing atoms by their index in the
Molecule. - Stereo
Mode - How stereochemistry is interpreted during InChI generation.
Functions§
- check_
inchi - Checks whether a string is a valid InChI.
- check_
inchikey - Checks whether a string is a syntactically valid InChIKey.
- from_
molfile - Generates an InChI directly from a Molfile (V2000/V3000 SDF text).
- inchi_
to_ inchi - Re-derives an InChI from an existing InChI string.
- inchikey
key - Computes the 27-character InChIKey for an InChI string.
- inchikey_
with_ hashes key - Computes the InChIKey and, optionally, its two 256-bit hash extensions.
- struct_
from_ aux_ info - Reconstructs a
Structurefrom the AuxInfo emitted alongside an InChI. - struct_
from_ inchi - Parses an InChI string into its atom/bond/stereo
Structure. - struct_
from_ inchi_ ex - Parses an InChI into its structure and polymer data via the extended
GetStructFromINCHIExentry point.