Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the use cases of raw identifiers besides new keywords?

Tags:

rust

rust-2018

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 a try 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?

like image 650
Tim Diekmann Avatar asked Jun 23 '18 10:06

Tim Diekmann


1 Answers

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.

like image 116
Lukas Kalbertodt Avatar answered Sep 25 '22 06:09

Lukas Kalbertodt