From 9863cd359d41bb0b76804a19668127c620366c63 Mon Sep 17 00:00:00 2001 From: Morgante Pell Date: Tue, 8 Oct 2024 03:12:58 -0500 Subject: [PATCH] feat: add rust import support --- .grit/patterns/rust/rust_imports.md | 58 +++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 .grit/patterns/rust/rust_imports.md diff --git a/.grit/patterns/rust/rust_imports.md b/.grit/patterns/rust/rust_imports.md new file mode 100644 index 00000000..90b55c55 --- /dev/null +++ b/.grit/patterns/rust/rust_imports.md @@ -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(); +} +```