简体   繁体   English

Rust引用(通常)Voldemort类型?

[英]Are Rust references (usually) Voldemort types?

Voldemort – he who must not be named – types are types whose names are impossible to write down in the source code. 伏地魔 - 不能被命名的人 - 类型是在源代码中无法写下名字的类型。 In Rust, closures have such types, because the compiler generates a new internal type for each closure . 在Rust中,闭包具有这样的类型,因为编译器为每个闭包生成一个新的内部类型 The only way to accept a closure as function argument is to accept a generic type (usually called F ) which is bounded to be an Fn() (or similar) trait. 接受闭包作为函数参数的唯一方法是接受泛型类型(通常称为F ),它被限制为Fn() (或类似)特征。

References in Rust always contain a lifetime parameter, even if this lifetime can usually be omitted. Rust中的引用始终包含一个生命周期参数,即使通常可以省略此生命周期。 Lifetimes can't be named explicitly, because they represent some complex compiler-internal scope of some kind. 生命周期不能明确命名,因为它们代表某种复杂的编译器内部范围。 The only way to interact with lifetimes is to use a generic parameter (usually called 'a ) which stands for any lifetime (maybe bounded by another lifetime). 与生命周期交互的唯一方法是使用通用参数(通常称为'a )代表任何生命周期(可能受另一个生命周期限制)。 Of course, there is 'static which can be named, but this is a special case and doesn't conflict with my arguing. 当然,有'static 可以命名,但这是一个特例,并不与我的争论相冲突。

So: are Rust references Voldemort types? 那么:Rust引用了Voldemort类型吗? Or do I misunderstand the term “Voldemort type” or Rust references? 或者我是否误解了“Voldemort类型”或Rust引用?

As someone without any particularly strong knowledge in the area: 作为在该地区没有任何特别强大知识的人:

I think the answer is probably: technically yes, but it's overly reductive . 我认为答案可能是: 技术上是,但它过度还原 A bit like saying "all types are arrays of integers"; 有点像说“所有类型都是整数数组”; I mean, yes , but you're losing some useful semantic discrimination by doing that. 我的意思是, 是的 ,但你通过这样做会失去一些有用的语义歧视。

Voldemort types are usually to hide the implementation type from the user, either because it's only supposed to be a temporary, or you're not supposed to use anything but the interface described by the function. Voldemort类型通常是隐藏用户的实现类型,因为它只应该是临时的,或者除了函数描述的接口之外你不应该使用任何东西。 References are technically unnameable in their entirety, but it's not like it ever actually restricts you. 引用在技术上完全不可命名,但它并不像它实际上限制你。 I mean, even if you could name the specific lifetime, I don't think you could do anything meaningful with it (except possibly for slightly stricter lifetime checking within a function). 我的意思是,即使您可以命名特定的生命周期,我也不认为您可以对它做任何有意义的事情(除非可能在函数内进行稍微严格的生命周期检查)。

Arguably no. 可以说没有。 Are the types of references and pointers in all languages considered Voldemort types? 所有语言中的引用和指针类型都被认为是Voldemort类型吗? They hide something , but no. 他们隐藏了一些东西 ,但没有。

We envision lifetimes as being regions of code outside the called function. 我们将生命期视为被调用函数之外的代码区域。 Also, they're created roughly like that in rustc. 而且,它们的创建大致类似于rustc。 Yet, I'd argue function signatures are the type definition of the lifetimes we actually see. 然而,我认为函数签名是我们实际看到的生命期的类型定义。 And rustc is merely satisfying them. 而rustc只是满足他们。 There is nothing more to the named lifetimes than what you see in the function definition. 命名生命周期没有比您在函数定义中看到的更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Rust 的元组中传递多个引用 - Pass multiple references in a tuple in Rust 在 Rust 中为静态生命周期的悬空引用和建议而苦苦挣扎 - Struggling with dangling references and suggestions for static lifetimes in Rust 惯用的Rust方法用于处理对缓冲区的引用 - Idiomatic Rust method for handling references to a buffer 创建递归枚举——我应该使用生命周期引用吗? (锈) - Creating recursive enum -- should I use lifetime references? (Rust) 将 const 引用传递给原始类型有什么用? - What is the use of passing const references to primitive types? 如何使用 Rust 在树结构中为单个节点提供多个引用 - How to have multiple references for a single node in a tree structure using Rust 为什么即使引用不是对象也有类型? - Why references have types even though they are not objects? 由于循环引用而确定如何订购F#类型的问题 - Problem determining how to order F# types due to circular references 如何在 Rust 中拥有多个可以访问其值的共享引用? - How does having multiple shared references which value can be accessed just works in Rust? 引用在C ++中是单独的类型吗? 如果没有,为什么要用这种字体写? - Are references separate types in C++? If not, why are they written in the type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM