Husk - Rust's syntax meets JavaScript's flexibility
Husk is a lightweight interpreted language that brings Rust's elegant syntax to the world of scripting and JavaScript development. Write type-safe, expressive code with the flexibility to run it instantly or compile it for production use with full access to npm's 1M+ packages.
Why Husk?
🚀 Best of Both Worlds
Write scripts with Rust's clean, expressive syntax while leveraging JavaScript's massive ecosystem. No more choosing between elegant code and practical libraries.
🎯 Choose Your Path
Interpreter Mode - Quick & Iterative
Perfect for:
- Rapid prototyping
- Build scripts
- CLI tools
- Learning and experimentation
# Zero configuration, instant feedback
# Interactive REPL for exploration
Transpiler Mode - Production Ready
Perfect for:
- Web applications
- npm packages
- Node.js services
- Existing JavaScript projects
# Build once, run anywhere JavaScript runs
# Use with your favorite npm packages
Features
Language Features
- Rust-inspired syntax - Familiar and expressive
- Static typing with inference - Catch errors early, write less boilerplate
- Pattern matching - Powerful control flow with match expressions
- Option & Result types - Built-in error handling the Rust way
- Structs & Enums - Model your data precisely
- Module system - Organize code across files
- Async/await - Modern asynchronous programming
- Type casting - Seamless conversions with
as
- Format macro - String interpolation done right
Developer Experience
- Interactive REPL - Test ideas instantly
- Zero configuration - Just write and run
- Project build system - Scale from scripts to applications
- JavaScript interop - Use any npm package
- Fast iteration - No compile times in interpreter mode
- Cross-platform - Runs wherever Node.js runs
Quick Start
Installation
# Install Husk (requires Rust/Cargo)
Your First Husk Program
// hello.husk
Run it instantly:
Or compile to JavaScript:
|
Usage Modes
REPL Mode
To start the Husk REPL, run:
Script Execution
To execute a Husk script file, use:
Transpilation to JavaScript
To transpile a Husk script to JavaScript, use:
This will output the transpiled JavaScript code to stdout. If you have node installed you can do:
|
Project Build System
Husk supports project-based development with husk.toml
configuration files. To build an entire project:
Additional build options:
# Generate package.json from husk.toml
# Specify target platform
Project Structure
Create a husk.toml
file in your project root:
[]
= "my-husk-app"
= "0.1.0"
= "My Husk application"
= "Your Name"
[]
= "^4.18.0"
= "^4.17.21"
[]
= "node-esm"
= "src"
= "dist"
Place your Husk source files in the src
directory. The build command will compile all .husk
files and generate corresponding JavaScript files in the output directory.
Real-World Example
Here's how Husk enables you to move seamlessly from rapid prototyping to production:
1. Start with the Interpreter (Rapid Development)
// calculator.husk - Quick prototype
# Test instantly during development
# Output: Result: 15
2. Transpile for Production (Access npm ecosystem)
// web-calculator.husk - Production version with Express
use express;
async
async
# Build for production
The same core logic, two different execution modes!
Language Syntax
Here are some examples of Husk syntax:
Variable Declaration
let x = 5;
let name = "Alice";
let is_true = true;
Function Definition
Struct Definition and Instantiation
let p = Person ;
Enum Definition and Pattern Matching
let opt = Option Some;
match opt
Arrays and Ranges
let arr = ;
let slice = arr;
for i in 0..5
Loops
// For loop
for x in
// While loop
let i = 0;
while i < 5
// Infinite loop with break
loop
Module System
Husk supports a module system for organizing code across multiple files:
// In utils.hk
// In main.husk
use ;
Module import prefixes:
local::
- Import from project rootself::
- Import from current directorysuper::
- Import from parent directory
Async/Await (Transpiler Only)
async
async
Built-in Types
Husk includes Option and Result types for safe error handling:
let result = divide?; // Error propagation operator
Type Casting
Husk supports explicit type conversion using the as
operator:
// Numeric conversions
let i = 42;
let f = i as float; // 42.0
let f2 = 3.14;
let i2 = f2 as int; // 3 (truncates decimal)
// String parsing
let s = "123";
let num = s as int; // 123
// To string conversion
let n = 456;
let str = n as string; // "456"
// Boolean conversion
let b = true as int; // 1
let zero = 0 as bool; // false
let nonzero = 5 as bool; // true
Built-in Methods
Husk provides JavaScript-compatible methods for primitive types:
String Methods
let s = " Hello World ";
println; // 15
println; // "Hello World"
println; // " hello world "
println; // " HELLO WORLD "
let text = "Hello World";
println; // "Hello"
println; // "World"
let csv = "apple,banana,orange";
let parts = csv.split; // ["apple", "banana", "orange"]
Array Methods
let arr = ;
println; // 5
Development
To set up the development environment:
-
Clone the repository:
-
Build the project:
-
Run tests:
Resources
- 🌐 Website: husk-lang.org
- 📚 Documentation: husk-lang.org/docs
- 🎮 Playground: husk-lang.org/playground
- 📖 Examples: husk-lang.org/examples
Community
- 💬 Discussions: GitHub Discussions
- 🐛 Issues: GitHub Issues
- 💡 Feature Requests: GitHub Issues
Contributing
Husk is an open-source project that welcomes contributions from everyone! Whether you're:
- 🐛 Reporting bugs
- 💡 Suggesting features
- 📝 Improving documentation
- 🔧 Writing code
- ✨ Sharing examples
We'd love your help making Husk better. Check out our Contributing Guide to get started.
Development
To set up the development environment:
# Clone the repository
# Build the project
# Run tests
# Install locally for testing
License
This project is licensed under the MIT License - see the LICENSE file for details.