xtruct
An anonymous struct literal macro for Rust. xtruct allows you to construct lightweight, ad-hoc struct types on the fly without declaring explicit struct types beforehand.
Features
- Zero boilerplate: Create anonymous structs inline with simple key-value syntax.
- Field shorthand: Infer fields directly from local variables in scope (e.g.,
xtruct! { name, count }). - Type inference: Automatically infers field types based on the passed expressions.
Usage Examples
Construction
use xtruct;
let user = xtruct! ;
assert_eq!;
assert_eq!;
assert_eq!;
Variable Field Shorthand
Like standard Rust struct initialization, you can use local variables directly without repeating the field name:
use xtruct;
let retry_count = 4usize;
let options = xtruct! ;
assert_eq!;
assert_eq!;
Scoped Anonymous Structs
Construct anonymous structs inline within local scopes or expressions:
use xtruct;
let payload = ;
assert_eq!;
assert_eq!;
Combining Expressions and Local Variables
Mix complex expressions and local variable shorthands in a single struct initialization:
use xtruct;
let user_id = 101;
let is_active = true;
let session = xtruct! ;
assert_eq!;
assert_eq!;
Nested Anonymous Structs
Instantiate nested anonymous structures by nesting xtruct! invocations:
use xtruct;
let config = xtruct! ;
assert_eq!;
assert_eq!;
Installation
Add xtruct to your Cargo.toml:
[]
= "0.1.0"
Or run:
License
This project is licensed under the MIT License.