As in Rust 2018, we now have raw identifiers:
This feature is useful for a few reasons, but the primary motivation was inter-edition situations. For example,
try
is not a keyword in the 2015 edition, but is in the 2018 edition. So if you have a library that is written in Rust 2015 and has atry
function, to call it in Rust 2018, you'll need to use the raw identifier.
Are there any other advantages than stated above? Are there plans to make keywords contextual, e.g. you can use type
as identifier for variables? Why I should use a cryptic syntax like r#type
instead of ty
or something else?
Why I should use a cryptic syntax like
r#type
instead ofty
or something else?
Sometimes the names of fields are used outside of your Rust program. For example, when serializing data with Serde, the field name is used in the output (e.g. JSON). So if you need JSON output with this:
"type": 27,
... then raw identifiers can help you:
#[derive(Serialize)]
struct Foo {
r#type: u32,
}
On the other hand... Serde already has a way to achieve what you want: the #[serde(rename = "name")]
attribute. Reserved Rust keywords are one of the reasons why this attribute was introduced.
#[derive(Serialize)]
struct Foo {
#[serde(rename = "type")]
ty: u32,
}
Similarly, the Debug
output also uses the field name in its output. So if you want the output Foo { type: 27 }
, you can use raw identifiers:
#[derive(Debug)]
struct Foo {
r#type: u32,
}
On the other hand... if the exact Debug
output is that important to you, you can simply implement it yourself:
impl fmt::Debug for Foo {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Foo")
.field("type", &self.ty)
.finish()
}
}
So in practice, I don't see why one would use raw identifier for this purpose, since you have to use the strange r#
syntax everywhere you use that name. It's probably easier to just fix this particular problem in another way.
So, as far as I see it, the "using API from another edition" is the only real use case for raw identifiers. Having such a syntax "just for the case" is a nice thing, though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With