Skip to content

rust

The Rust language support module provides a rich set of voice commands, type definitions, macros, and operators tailored for writing idiomatic Rust code. It works by combining a Python-based engine that defines Rust's structure and syntax rules with a Talon file that maps voice triggers to specific code behaviors.

Together, these files hook into Talon's generic code-editing tags, overriding them with Rust-specific implementations when a Rust source file is active.

How the Files Work Together

  • rust.py manages the logic, lists, custom actions, and syntax rules. It defines scalar and compound types, standard library types, macro classifications, and operator translations (such as using .pow() for exponents or & for addresses).
  • rust.talon activates when code.language: rust is detected. It registers basic coding tags (like OOP, imperative, and C-like comments), sets naming conventions (e.g., SNAKE_CASE for functions), and defines voice commands for keywords, structures, enums, pattern matching, and turbofish operators.

Key Features and Implementation

1. Formatting and Naming Conventions

The module configures standard Rust formatting rules automatically. In rust.talon, settings are defined to ensure:

  • Functions (private, protected, public) and variables default to SNAKE_CASE.
  • Structures and enums are created using PUBLIC_CAMEL_CASE dynamically from the spoken input.

2. Broad Type System Support

rust.py contains mappings for virtually all native and standard library Rust types. These are grouped into:

  • Scalar types: Map spoken phrases like "eye eight" to i8, "you size" to usize, and "float sixty four" to f64.
  • Compound types: Map "tuple" to () and "array" to [].
  • Standard library / sync types: Provide quick access to Box, Vec, String, Option, Result, HashMap, as well as concurrency primitives like Arc, Mutex, and RwLock.

Modifiers like mut, &, and &mut are supported via the code_type_modifier list, allowing users to declare complex types like "borrowed mutable vector" seamlessly.

3. Macro and Trait Handling

Rust relies heavily on macros and traits, both of which are given specialized treatment in this directory:

  • Context-Aware Macros: rust.py distinguishes between function-style macros (panic!), array-style macros (vec!), and block-style macros (macro_rules!). The custom code_insert_macro action automatically formats the trailing brackets based on these categories—such as inserting () for println! and [] for vec!.
  • Traits: A dictionary of common traits (such as closure traits Fn, FnOnce, and iterator/conversion traits) simplifies writing generics and trait bounds. rust.talon provides syntax modifiers like "returns implemented trait" (-> impl) or "dynamic trait" (dyn).

4. Control Flow and Idiomatic Patterns

rust.talon supplies short voice patterns for common Rust flow controls and idiomatic expressions:

  • Pattern Matching: Voice triggers like "state if let some" and "state if let error" expand into if let Some(...) and if let Err(...), positioning the cursor cleanly.
  • Null Safety: Maps standard null operations (code_insert_null, code_insert_is_null) to Option-based variants (None, .is_none(), and .is_some()).
  • Turbofish: Calling "turbo fish" outputs the distinctive ::<> syntax and positions the cursor inside.

5. Documentation and Unsafe Blocks

For special block structures, rust.py defines helper commands that invoke editor snippets or raw text insertion:

  • Unsafe: Speaking "unsafe block" leverages a structural snippet to generate a formatted unsafe {} scope.
  • Documentation Comments: Supports block documentation (/** ... */) and inner module/crate comments (//! and /*! ... */) with automated newline-aware cursor placement.