{"id":2327,"library":"tree-sitter-rust","title":"Tree-sitter Rust Grammar for Python","description":"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.","status":"active","version":"0.24.2","language":"en","source_language":"en","source_url":"https://github.com/tree-sitter/tree-sitter-rust","tags":["tree-sitter","rust","parsing","grammar","ast","compiler","syntax-tree","code-analysis"],"install":[{"cmd":"pip install tree-sitter tree-sitter-rust","lang":"bash","label":"Install core library and Rust grammar"}],"dependencies":[{"reason":"Provides the core parsing engine and Python bindings necessary to use any Tree-sitter grammar.","package":"tree-sitter","optional":false}],"imports":[{"note":"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.","wrong":"from tree_sitter_rust import Language","symbol":"language","correct":"from tree_sitter_rust import language"}],"quickstart":{"code":"import tree_sitter\nfrom tree_sitter import Language, Parser\nfrom tree_sitter_rust import language\n\n# Example Rust code to parse\nrust_code = '''\nfn main() {\n    let message = \"Hello, world!\";\n    println!(\"{}\", message);\n}\n'''\n\n# 1. Load the Rust language grammar\n# The `language` object imported is a callable that returns the actual LanguageFn.\nRUST_LANGUAGE = Language(language())\n\n# 2. Create a parser and set its language\nparser = Parser()\nparser.set_language(RUST_LANGUAGE)\n\n# 3. Parse the code (input must be bytes)\ntree = parser.parse(bytes(rust_code, \"utf8\"))\n\n# 4. Get the root node of the syntax tree\nroot_node = tree.root_node\n\nprint(f\"Root node type: {root_node.type}\")\nprint(f\"Root node text: {root_node.text}\")\nprint(f\"Tree S-expression: {root_node.sexp()}\")\n\n# Example of traversing the tree\nprint(\"\\nWalking the tree nodes:\")\ncursor = tree.walk()\n\n# Start at the root\ndef traverse(cursor, indent=0):\n    print(\"  \" * indent + f\"Node: {cursor.node.type} [start: {cursor.node.start_point}, end: {cursor.node.end_point}]\")\n    if cursor.goto_first_child():\n        traverse(cursor, indent + 1)\n        while cursor.goto_next_sibling():\n            traverse(cursor, indent + 1)\n        cursor.goto_parent() # Go back up after visiting all siblings\n\ntraverse(cursor)","lang":"python","description":"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."},"warnings":[{"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')`.","message":"The `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.","severity":"breaking","affected_versions":"tree-sitter Python bindings versions >= 0.22.0"},{"fix":"Ensure both `tree-sitter` and `tree-sitter-rust` are installed: `pip install tree-sitter tree-sitter-rust`.","message":"The `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.","severity":"gotcha","affected_versions":"All versions of `tree-sitter-rust`"},{"fix":"Encode the input string to bytes, typically using UTF-8: `parser.parse(bytes(code_string, 'utf8'))`.","message":"The `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`.","severity":"gotcha","affected_versions":"All versions"},{"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`.","message":"Incompatibilities 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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}