[−][src]Macro label_macros::create_label
create_label!() { /* proc-macro */ }
Creates a new label.
create_label!(fn test() -> ());
To use a label, add an attribute to a function in the following style:
#[test::label] fn my_function() { // contents }
test
is the name of your label (this has to be a full path to it. Labels can be imported).
The annotation has to end with ::label
, or otherwise it will not compile.
It is possible to create multipe labels in one invocation of the create_label!()
macro. The syntax for this is as follows:
create_label!( fn test() -> (); fn test1(usize) -> (usize); fn test2(usize) -> (isize); );
It is not supported to have two labels in scope with the same name, just like two structs in the same scope with the same name won't work either.
1 After a label is created, it is possible to iterate over all functions annotated with this label, using the iter function:
for func in test::iter() { // do something with the function func(); }
The order in which iteration occurs is not defined.
Alternatively, you can iterate over functions and their names using the iter_named()
function:
for (name, func) in test::iter_named() { println!("name: {}", name); // do something with the function func(); }
Labels can also be given to static
or const
variables. Iterating over such labeled variables
returns an &'static
reference to the variable. You can define variable labels with
create_label!()
. It does not matter if you use const
or static
, they are handled the same.
static mut
is supported, though iterating over labels will never allow you to mutate these
variables. static mut
in create_label!()
does nothing. If a static mut
is locally updated,
and the label is iterated over, the changed value is reflected.
create_label!( const name: usize; static other_name: usize; );
for i in name::iter() { println!("value: {}", *i); }