macros - Rust method that returns token name as a string -


the stringify macro returns string of token passed it:

struct a;  fn main() {     let my_identifier = {};     assert!(stringify!(my_identifier) == "my_identifier"); } 

[playground]

is there way method return string of token on called?

something like:

struct a;  impl {     fn token_to_string(&self) -> &str{         /* ... */     } }  fn main() {     let my_identifier = {};     assert!(my_identifier.token_to_string() == "my_identifier"); } 

[playground]

googling has not been fruitful. i'm not sure if it's possible, thought reasonable ask here before diving implementation of stringify next step in investigating this.

no, not possible. these 2 constructs execute @ different times in different contexts.

macros execute during process of compilation, , have access of information original source files. means able process individual source tokens , perform operations based on them.

methods execute when program runs, , have access arguments, , global variables. have no data original sourcecode of application, because information not stored in compiled binary.


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -