1

I have a struct like this:

struct Input {
   key1: u32,
   key2: String,
   value1: u16,
   value2: u8
}

I want to write a proc macro using quote that generates this struct:

struct InputKey {
    key1: u32,
    key2: String,
}

How do I do this?

The most I could figure out is this:

let ident_key_type = quote::format_ident!("{}Key", ident);
quote! {
    pub struct #ident_key_type {
    }
}

This is creating an empty struct.

I am unable to figure out how to write a for-loop to iterate over the 2 key fields "Key1" and "key2" and add those 2 fields to the InputKey struct.

My code uses the quote library so I would prefer a solution that uses that.

Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77
user855
  • 19,048
  • 38
  • 98
  • 162
  • 2
    Have you seen the readme on [Repetition](https://crates.io/crates/quote#repetition)? You'd have to create a vec/iterator that contains `quote!{ keyN: typN, }` as elements for that. – Caesar Aug 30 '22 at 02:53
  • Are there always `key1: u32` and `key2: String`? Or could it be `n` key fields of any type `T`? – mori Oct 01 '22 at 07:39

0 Answers0