What is the meaning of $ in that code?

::std::option::Option::Some(f(&mut self.$v))

Thank you

What's the context you got the code from ?
$ usually is used in macro_rules contexts.
I recommended https://cheats.rs/ for getting the feel of Rust syntaxs.

Thanks, have to read about macros.
Coming from C++ have some dislike towards them though...

Well, you might like (or dislike) the fact that Rust macros are not like C(++) macros, but more like C++ templates.

1 Like

O, that's interesting because I really like C++ templates! Thanks!

In particular, Rust macros are not dumb raw text substitution operators. They operate on a semi-parsed, structured representation of the source code called a token tree, which is between tokens and the abstract syntax tree in terms of abstraction/expressive power.

Because macros operate over a structured representation, not raw strings, it's largely impossible to make the same kind of simple dumb mistakes with them that you would be prone to when working with the C preprocessor. For example, consider the following, likely incorrect C code:

#define ADD(x, y) x + y
#define MUL(x, y) x * y

// this is expanded to 2 * 3 + 4 = 10, even
// though it was expected to be 2 * (3 + 4) = 14
int z = MUL(2, ADD(3, 4));

The equivalent Rust code is spliced correctly:

macro_rules! add {
    ($x:expr, $y:expr) => { $x + $y }
}
macro_rules! mul {
    ($x:expr, $y:expr) => { $x * $y }
}

let z = mul!(2, add!(3, 4)); // z == 14
8 Likes