replace_token_sequence!() { /* proc-macro */ }
Expand description
Replaces all occurences of a given sequence of tokens with another sequence of tokens.
Takes arguments in the form replace_token!{[needle tokens], [replacement tokens], code to search}
.
The brackets around the needle and replacement tokens are not included in the search.
You may use any kind of brackets.
§Panics
At compile time, if the arguments cannot be correctly parsed.
§Examples
Replaces the sequence 9 + 10
with 21
. Note that spacing between tokens is generally ignored:
replace_token_sequence!{[9 + 10], [21],
let x = 9+10;
}
assert_eq!(x, 21);
Replaces the content of a match arm:
enum Numbers {
I32(i32),
I64(i64),
ISize(isize)
}
let n = Numbers::I32(12);
replace_token_sequence!{[MY_ARM], [(y * 2) as f64],
let x = match(n) {
Numbers::I32(y) => MY_ARM,
Numbers::I64(y) => MY_ARM,
Numbers::ISize(y) => MY_ARM,
};
}
assert_eq!(x, 24.0);