Slide 7
Slide 7 text
© 2023 estie Inc.
# Cargo.toml
[package]
name = "proc-macro-test"
version = "0.1.0"
edition = "2021"
[lib]
proc-macro = true
[dependencies]
// lib.rs
use proc_macro::{Delimiter, Group, Ident, Punct, Spacing, Span, TokenStream, TokenTree};
#[proc_macro]
pub fn print_twice(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
TokenTree::from(Group::new(
Delimiter::Brace,
vec![
vec![
TokenTree::from(Ident::new("println", Span::call_site())),
TokenTree::from(Punct::new('!', Spacing::Alone)),
TokenTree::from(Group::new(Delimiter::Parenthesis, input.clone())),
]
.into_iter()
.collect::(),
TokenTree::from(Punct::new(';', Spacing::Alone)).into(),
vec![
TokenTree::from(Ident::new("println", Span::call_site())),
TokenTree::from(Punct::new('!', Spacing::Alone)),
TokenTree::from(Group::new(Delimiter::Parenthesis, input.clone())),
]
.into_iter()
.collect::(),
]
.into_iter()
.collect::(),
))
.into()
}
*あとでわかりやすい書き方説明するので
あんまり気にしなくて大丈夫です