Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add rust import support #241

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
```
Loading