To use the Rust grammar in a Node.js environment, require the tree-sitter-rust package. The package automatically selects the correct prebuilt binary based on your platform and architecture using node-gyp-build. It also provides support for Bun via static analysis of the .node file path.
Note: This module exports the compiled grammar object which can be used with the main tree-sitter library to parse Rust code.
const Parser = require('tree-sitter');
const Rust = require('tree-sitter-rust');
const parser = new Parser();
parser.setLanguage(Rust);