Why I should use a cryptic syntax like r#type
instead of ty
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.