Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/tree-sitter/tree-sitter/llms.txt

Use this file to discover all available pages before exploring further.

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:
[dependencies]
tree-sitter = "0.24"
To use a language grammar, add it as a dependency:
[dependencies]
tree-sitter = "0.24"
tree-sitter-rust = "0.23"

Basic Usage

First, create a parser:
use tree_sitter::{InputEdit, Language, Parser, Point};

let mut parser = Parser::new();
Then, assign a language to the parser:
parser.set_language(&tree_sitter_rust::LANGUAGE.into())
    .expect("Error loading Rust grammar");
Now you can parse source code:
let source_code = "fn test() {}";
let mut tree = parser.parse(source_code, None).unwrap();
let root_node = tree.root_node();

assert_eq!(root_node.kind(), "source_file");
assert_eq!(root_node.start_position().column, 0);
assert_eq!(root_node.end_position().column, 12);

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:
LANGUAGE_VERSION
usize
The latest ABI version supported by the current version of the library
MIN_COMPATIBLE_LANGUAGE_VERSION
usize
The earliest ABI version supported by the current version of the library

Features

The crate supports several optional features:
std
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
wasm
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:
pub enum LanguageError {
    Version(usize),
    #[cfg(feature = "wasm")]
    Wasm,
}

pub struct QueryError {
    pub row: usize,
    pub column: usize,
    pub offset: usize,
    pub message: String,
    pub kind: QueryErrorKind,
}

pub enum QueryErrorKind {
    Syntax,
    NodeType,
    Field,
    Capture,
    Predicate,
    Structure,
    Language,
}

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