Splicing
Backticks mean “evaluate this now, and put the result here.”
`expression`
There are two uses:
- Building names.
r`i`withi = 3is the namer3. This is the most common use, covered in Spliced names. - Freezing a value into code that runs later. A declaration generated by a macro is evaluated where it ends up, after the macro’s parameters are gone. A splice evaluates part of it while they still exist. See Generating declarations.
macro declare_scaled(n: int) {
pub const SCALED`n` = `n * 100`
}
declare_scaled 7
macro show(value: int) {
@emit value
}
show SCALED7
700
Here the name SCALED`n` becomes SCALED7, and the value `n * 100`
becomes 700, both while declare_scaled runs.
Where it makes no difference
In ordinary expressions, which are evaluated anyway, a splice changes nothing:
`1 + 2` is the same as 1 + 2.
macro show(value: int) {
@emit value
}
show `1 + 2`
3
A literal backtick
To write a backtick that isn’t a splice, for example in a
custom syntax pattern, escape it: \`.