rustyjs
v1.1.1
Published
A library that brings Rust features to JavaScript
Downloads
3
Maintainers
Readme
About
RustyJS aims to bridge the gap between Rust and JavaScript, allowing developers to leverage Rust's powerful features in their JavaScript projects. By providing utilities inspired by Rust's syntax and concepts, RustyJS enhances code safety, error handling, and overall developer productivity.
Features
- Option and Result Types: Implementations of Rust's Option and Result types for safer handling of nullable values and error propagation.
- Iterators: Implementation of Rust's Iterators allowing for more powerful traversal of series of data.
Installation
You can install RustyJS via npm:
npm install rustyjs
Getting Started
To get started with RustyJS, you can refer to the following examples:
Example: Option and Result types
import { Option, Result } from 'rustyjs';
// Using Option
const maybeNumber = Option.Some(42);
const result = maybeNumber.map(num => num * 2).unwrapOr(0);
console.log(result); // Output: 84
// Using Result
const divide = (a, b) => {
if (b === 0) {
return Result.Err(new Error("Division by zero"));
}
return Result.Ok(a / b);
};
const result1 = divide(10, 2).unwrap();
console.log(result1); // Output: 5
const result2 = divide(10, 0).unwrapOr(0);
console.log(result2); // Output: 0
const result3 = divide(20, 3);
if (result3.isSome()) {
console.log(result3.unwrap()); // unwrap can be safely called here
}