Skip to main content
The Tree-sitter Rust bindings provide idiomatic Rust access to the Tree-sitter parsing library. The bindings are available on crates.io and provide a safe, ergonomic API for parsing and analyzing source code.

Installation

Add Tree-sitter to your Cargo.toml:
To use a language grammar, add it as a dependency:

Basic Usage

First, create a parser:
Then, assign a language to the parser:
Now you can parse source code:

Core Types

The Rust bindings expose several key types:

Parser

Create and configure parsers for generating syntax trees

Tree

Represent and manipulate syntax trees

Node

Navigate and inspect individual nodes in a syntax tree

Query

Search syntax trees using pattern matching

Language

Define and inspect language grammars

Wasm

Load and use WebAssembly-based grammars

ABI Compatibility

The Rust bindings include version constants for checking ABI compatibility:
usize
The latest ABI version supported by the current version of the library
usize
The earliest ABI version supported by the current version of the library

Features

The crate supports several optional features:
feature
default:"enabled"
Enables standard library support. When enabled:
  • Error types implement std::error::Error
  • regex performance optimizations are enabled
  • DOT graph methods are available
feature
Enables WebAssembly support for loading grammar files compiled to Wasm. Requires the wasmtime-c-api crate.

Error Handling

The bindings use idiomatic Rust error handling with Result types:

Next Steps

Parser API

Learn how to create and configure parsers

Tree Traversal

Navigate and inspect syntax trees

Query Patterns

Search syntax trees with queries

Wasm Support

Use WebAssembly-based grammars