除新关键字外,原始标识符的用例还有哪些? [英] What are the use cases of raw identifiers besides new keywords?

查看:119
本文介绍了除新关键字外,原始标识符的用例还有哪些?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与Rust 2018一样,我们现在有了原始标识符:

As in Rust 2018, we now have raw identifiers:

出于某些原因,此功能很有用,但主要动机是版本间的情况.例如,try在2015年版中不是关键字,但在2018年版中是关键字.因此,如果您有一个用Rust 2015编写并具有try函数的库,要在Rust 2018中调用它,则需要使用原始标识符.

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.

除了以上所述,还有其他优势吗?是否有计划使关键字具有上下文相关性,例如您可以使用type作为变量的标识符吗?为什么我应该使用r#type之类的神秘语法而不是ty之类的东西?

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?

推荐答案

为什么我应该使用r#type之类的神秘语法而不是ty之类的东西?

Why I should use a cryptic syntax like r#type instead of ty or something else?

有时字段名称在Rust程序之外使用.例如,当使用Serde序列化数据时,在输出中使用字段名称(例如JSON).因此,如果您需要与此相关的JSON输出:

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,
}

另一方面,Serde已经可以实现所需的方法:属性.保留的Rust关键字是引入此属性的原因之一.

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,
}


类似地,Debug输出在其输出中也使用字段名称.因此,如果要输出Foo { type: 27 },则可以使用原始标识符:


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,
}

另一方面...如果确切的Debug输出对您来说如此重要,则您可以自己实施:

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()
    }
}


因此在实践中,我不知道为什么要为此目的使用原始标识符,因为在每次使用该名称的地方都必须使用奇怪的r#语法.仅以另一种方式解决此特定问题可能会更容易.


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.

因此,据我所知,使用其他版本的API"是原始标识符的唯一实际用例.具有这样的语法仅适用于案例"是一个很好的选择事情.

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.

这篇关于除新关键字外,原始标识符的用例还有哪些?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆