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

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -