#proc-macro #struct #iterable


A Rust library providing a proc macro to make a struct iterable

1 unstable release

0.1.1 May 10, 2023
0.1.0 May 10, 2023

#1007 in Rust patterns

Download history 609/week @ 2023-08-13 1349/week @ 2023-08-20 933/week @ 2023-08-27 747/week @ 2023-09-03 938/week @ 2023-09-10 1343/week @ 2023-09-17 995/week @ 2023-09-24 1300/week @ 2023-10-01 1221/week @ 2023-10-08 1581/week @ 2023-10-15 1535/week @ 2023-10-22 1165/week @ 2023-10-29 1351/week @ 2023-11-05 1149/week @ 2023-11-12 996/week @ 2023-11-19 966/week @ 2023-11-26

4,570 downloads per month
Used in 8 crates (3 directly)

MIT license


Struct Iterable

Struct Iterable is a Rust library that provides a proc macro to make a struct iterable. This allows you to iterate over the fields of your struct in a generic way, with each iteration returning a tuple containing the name of the field as a static string and a reference to the field's value as a dyn Any.

How to Use

First, add Struct Iterable to your Cargo.toml:

struct_iterable = "0.1.1"

Next, include the library at the top of your Rust file:

use struct_iterable::Iterable;

Finally, add the #[derive(Iterable)] attribute to your struct:

struct MyStruct {
    field1: u32,
    field2: String,
    // etc.

Now, you can iterate over the fields of an instance of your struct:

let my_instance = MyStruct {
    field1: 42,
    field2: "Hello, world!".to_string(),

for (field_name, field_value) in my_instance.iter() {
    println!("{}: {:?}", field_name, field_value);


  • Only structs with named fields are supported.
  • Only structs are supported, not enums or unions.


Here is the implementation of the proc macro:

extern crate proc_macro;

use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, Data, DeriveInput, Fields};
use iterable_structs::Iterable;

pub fn derive_iterable(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);

    let struct_name = input.ident;
    let fields = match input.data {
        Data::Struct(data_struct) => match data_struct.fields {
            Fields::Named(fields_named) => fields_named.named,
            _ => panic!("Only structs with named fields are supported"),
        _ => panic!("Only structs are supported"),

    let fields_iter = fields.iter().map(|field| {
        let field_ident = &field.ident;
        let field_name = field_ident.as_ref().unwrap().to_string();
        quote! {
            (#field_name, &(self.#field_ident) as &dyn std::any::Any)

    let expanded = quote! {
        impl Iterable for #struct_name {
            fn iter<'a>(&'a self) -> std::vec::IntoIter<(&'static str, &'a dyn std::any::Any)> {


The macro takes in the TokenStream of a struct and expands it into an implementation of the Iterable trait for that struct. This trait provides an iter method that returns an iterator over tuples of field names and values.

Contributing and License

Struct Iterable is an open-source project, and contributions are warmly welcomed. Whether you're fixing bugs, improving the documentation, or proposing new features, your efforts are highly appreciated!

If you're interested in contributing, please feel free to submit a pull request. For major changes, please open an issue first to discuss what you would like to change.

Please note that this project is released with a Contributor Code of Conduct. By participating in this project, you agree to abide by its terms.

Struct Iterable is distributed under the terms of the MIT license. As such, you're free to use, modify, distribute, and privately use it in any way you see fit, in accordance with the terms of the license.


~26K SLoC