dtolnay / quote

Rust quasi-quoting
Apache License 2.0
1.32k stars 90 forks source link

Vec<String> is not valid Ident #234

Closed wexgjduv closed 1 year ago

wexgjduv commented 1 year ago

Hi, I'm using quote to generate struct for fields with Vec<String> as type, there's an error for the following code:

fn make_ident(s: &str) -> Ident {
    syn::Ident::new(s, s.span())
}

fn main() {
    let mut code = quote!();
    #[allow(nonstandard_style)]
    let struct_name = "MyStruct";

    let (field, typ) = ("Name", "Vec<String>");
    let struct_name = make_ident(struct_name);
    let field = make_ident(&field);
    let typ = make_ident(&typ);
    code.extend(quote! {
        struct #struct_name {
            // TODO: iter all field
            #field: #typ,
        }
    });
}

The error shows:

thread 'main' panicked at '"Vec<String>" is not a valid Ident',

What is correct way to generate struct's fields with type Vec<String> or HashMap<String, String>?

Thanks

dtolnay commented 1 year ago

This is definitely behaving correctly. You can find an explanation of what identifier means here: https://doc.rust-lang.org/1.65.0/reference/identifiers.html