📖 About
This demo showcases the conversions_rs Rust library compiled to WebAssembly.
The library provides comprehensive unit conversions for SI base units and derived units including
length, weight, temperature, volume, time, electric current, amount of substance, luminous intensity, and
area.
Loading WebAssembly module...
💻 JavaScript Usage Examples
Here's how to use the WASM module in your JavaScript code:
Basic Import and Initialization
import init, {
convert_length_wasm,
convert_weight_wasm,
convert_temperature_wasm,
get_supported_units
} from './pkg/web/conversions_rs.js';
// Initialize the WASM module
await init();
// Now you can use the conversion functions
const result = convert_length_wasm(100, "ft", "m");
if (result.success) {
console.log(`100 feet = ${result.value} meters`);
} else {
console.error("Conversion failed:", result.error);
}
Error Handling
const result = convert_weight_wasm(10, "kg", "invalid_unit");
if (!result.success) {
console.error("Error:", result.error);
// Handle the error appropriately
}
Getting Supported Units
const lengthUnits = get_supported_units("length");
console.log("Supported length units:", lengthUnits);
// Output: ["m", "km", "cm", "mm", "ft", "in", "yd", "mi", ...]