Registry / serialization / tree-sitter-rust

tree-sitter-rust

JSON →
library0.24.2pypypi✓ verified 29d ago

tree-sitter-rust provides the official Rust grammar for the Tree-sitter parsing library in Python. It enables fast, incremental parsing of Rust code, generating concrete syntax trees for advanced code analysis, tooling, and editor integrations. The library is actively maintained, with frequent releases to keep up with `tree-sitter` core and Rust language changes.

pip install tree-sitter tree-sitter-rust
INSTALL
IMPORT
SIG · TREE-SITTER-RUST
T
tree-sitter-rust
serializationpythonv0.24.2
Install
1.7s avg
Import
48ms
Disk
19MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v0.24.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.050s · 20.5MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 1.7s · import 0.046s · 22MB
19MB installed
● package 19MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

language
✓ from tree_sitter_rust import language
✗ from tree_sitter_rust import Language
The `tree-sitter-rust` package exposes a `language` callable (which returns the `LanguageFn` object), not a `Language` class directly. This callable is then passed to `tree_sitter.Language()` to initialize the grammar for the parser.

This quickstart demonstrates how to initialize a Tree-sitter parser with the Rust grammar, parse a simple Rust code snippet, and then inspect the resulting syntax tree's root node and traverse its structure.

import tree_sitter from tree_sitter import Language, Parser from tree_sitter_rust import language # Example Rust code to parse rust_code = ''' fn main() { let message = "Hello, world!"; println!("{}", message); } ''' # 1. Load the Rust language grammar # The `language` object imported is a callable that returns the actual LanguageFn. RUST_LANGUAGE = Language(language()) # 2. Create a parser and set its language parser = Parser() parser.set_language(RUST_LANGUAGE) # 3. Parse the code (input must be bytes) tree = parser.parse(bytes(rust_code, "utf8")) # 4. Get the root node of the syntax tree root_node = tree.root_node print(f"Root node type: {root_node.type}") print(f"Root node text: {root_node.text}") print(f"Tree S-expression: {root_node.sexp()}") # Example of traversing the tree print("\nWalking the tree nodes:") cursor = tree.walk() # Start at the root def traverse(cursor, indent=0): print(" " * indent + f"Node: {cursor.node.type} [start: {cursor.node.start_point}, end: {cursor.node.end_point}]") if cursor.goto_first_child(): traverse(cursor, indent + 1) while cursor.goto_next_sibling(): traverse(cursor, indent + 1) cursor.goto_parent() # Go back up after visiting all siblings traverse(cursor)
Debug
Known issues
breakingThe `Language.build_library()` method, used for compiling Tree-sitter grammars from source at runtime, was removed from the `tree-sitter` Python library (formerly `py-tree-sitter`) around version 0.22. This means on-the-fly grammar compilation is no longer directly supported by the Python bindings.
fix
For pre-packaged grammars like `tree-sitter-rust`, use `pip install` which provides pre-compiled binaries. For custom grammars, compile them manually to a `.so` (or `.dll`/`.dylib`) file using the `tree-sitter` CLI tool and load with `tree_sitter.Language('/path/to/grammar.so', 'grammar_name')`.
affects: tree-sitter Python bindings versions >= 0.22.0
gotchaThe `tree-sitter-rust` package provides only the Rust grammar definition; it relies on the core `tree-sitter` Python library for the parser engine itself. Forgetting to install `tree-sitter` will lead to `ModuleNotFoundError` or `RuntimeError` when attempting to use the parser.
fix
Ensure both `tree-sitter` and `tree-sitter-rust` are installed: `pip install tree-sitter tree-sitter-rust`.
affects: All versions of `tree-sitter-rust`
gotchaThe `parser.parse()` method expects a `bytes` object as input for the source code, not a `str`. Passing a Python string directly will result in a `TypeError`.
fix
Encode the input string to bytes, typically using UTF-8: `parser.parse(bytes(code_string, 'utf8'))`.
affects: All versions
gotchaIncompatibilities between the `tree-sitter` core library version and the `tree-sitter-rust` grammar version can lead to runtime errors or incorrect parsing, especially if mixing installation methods or manually compiling grammars.
fix
Always install both `tree-sitter` and `tree-sitter-rust` via `pip` to ensure compatible pre-compiled versions are used. If issues persist, try updating both: `pip install --upgrade tree-sitter tree-sitter-rust`.
affects: All versions
Upgrade
Version history
0.24.2latest on PyPI · released Mar 27, 2026
Audit
Dependencies
tree-sitterrequiredProvides the core parsing engine and Python bindings necessary to use any Tree-sitter grammar.
Agent activity
17 hits · last 30 days
node
14
OpenAI (training)
1
Resources
tree-sitter-rust — pip install tree-sitter-rust · libregistry