Skip to content

Commit

Permalink
feat: add rust import support
Browse files Browse the repository at this point in the history
  • Loading branch information
morgante committed Oct 8, 2024
1 parent f04a793 commit 9863cd3
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions .grit/patterns/rust/rust_imports.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
title: Rust import management
tags: [docs, full-examples]
---

Grit includes a standard `add_import` pattern for adding imports to Rust files.

## `add_import($module, $identifier)` pattern

The `add_import` pattern is used to add a `use` declaration to the top of a Rust file.

```grit
language rust
`$body` where {
add_import(source="std", name="collections::HashMap")
}
```

This pattern will add the import to the top of the file, if it is not already present. For example:

Before:

```rust
fn main() {
let map = HashMap::new();
}
```

After:

```rust
use std::collections::HashMap;

fn main() {
let map = HashMap::new();
}
```

If other imports are present from the same module, they will be added in the same declaration.

```rust
use std::collections::{HashSet};


fn main() {
let map = HashMap::new();
}
```

```rust
use std::collections::{HashSet, HashMap};


fn main() {
let map = HashMap::new();
}
```

0 comments on commit 9863cd3

Please sign in to comment.