> ## Documentation Index
> Fetch the complete documentation index at: https://differens.ossl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Rust

> Semantic diffing for Rust

## Rust extractor

The Rust extractor maps tree-sitter Rust CST nodes to canonical concepts. It detects structural changes at the item level: functions, structs, enums, traits, and their members.

### Detected concepts

| tree-sitter node type         | Canonical concept  |
| ----------------------------- | ------------------ |
| `function_item`               | Function           |
| `struct_item`                 | Struct             |
| `enum_item`                   | Enum               |
| `trait_item`                  | Trait              |
| `impl_item`                   | Implementation     |
| method inside `impl`          | Method             |
| `use_declaration`             | Import             |
| `mod_item`                    | Module declaration |
| `macro_definition`            | Macro              |
| `let_declaration` (top-level) | Variable           |
| `const_item`                  | Constant           |
| `type_item`                   | Type alias         |

### Example

Two versions of a Rust module. One renames a function and adds a struct field:

```rust theme={null}
// old.rs
pub fn parse_config(path: &str) -> Config {
    let contents = std::fs::read_to_string(path)?;
    toml::from_str(&contents).unwrap()
}

pub struct Config {
    pub workers: u32,
}
```

```rust theme={null}
// new.rs
pub fn load_config(path: &str) -> Result<Config, Error> {
    let contents = std::fs::read_to_string(path)?;
    toml::from_str(&contents).map_err(Error::from)
}

pub struct Config {
    pub workers: u32,
    pub timeout_ms: u64,
}
```

Running `differens old.rs new.rs`:

```
  ~ renamed function `parse_config` to `load_config`
  ~ changed value of return type from `Config` to `Result<Config, Error>`
  + added field `timeout_ms` in struct `Config`
3 modifications
```

### Known limitations

* Macro expansion is not followed. The extractor works on the surface AST, so code generated by macros (`derive`, proc macros) is invisible to the diff.
* `impl` blocks are matched to their type, but orphan impls (impl blocks in separate files) require cross-file correlation to be active.
* Attribute changes (`#[derive(...)]`, `#[cfg(...)]`) are reported at the item level, not as separate edit actions.
