简体   繁体   English

Rust的所有权语义如何与Clean和Mercury中的唯一性类型相关?

[英]How do Rust's ownership semantics relate to uniqueness typing as found in Clean and Mercury?

I noticed that in Rust moving is applied to lvalues, and it's statically enforced that moved-from objects are not used. 我注意到在Rust中移动应用于左值,并且静态强制执行不移动的对象。

How do these semantics relate to uniqueness typing as found in Clean and Mercury? 这些语义如何与Clean和Mercury中的唯一性类型相关? Are they the same concept? 它们是相同的概念吗? If not, how do they differ? 如果没有,它们有何不同?

The concept of ownership in Rust is not the same as uniqueness in Mercury and Clean, although they are related in that they both aim to provide safety via static checking, and they are both defined in terms of the number of references within a scope. Rust中的所有权概念与Mercury和Clean中的唯一性不同,尽管它们的相关性在于它们都旨在通过静态检查提供安全性,并且它们都是根据范围内的引用数量来定义的。 The key differences are: 关键的区别是:

  • Uniqueness is a more abstract concept. 唯一性是一个更抽象的概念。 While it can be interpreted as saying that a reference to a memory location is unique, like Rust's lvalues , it can also apply to abstract values such as the state of every object in the universe, to give an extreme but typical example. 虽然可以解释为对内存位置的引用是唯一的,如Rust的左值 ,但它也可以应用于抽象值,例如宇宙中每个对象的状态,以给出一个极端但典型的例子。 There is no pointer corresponding to such a value - it cannot be opened up and inspected within a debugger or anything like that - but it can be used through an interface just like any other abstract type. 没有与这样的值对应的指针 - 它不能在调试器或类似的东西中打开和检查 - 但它可以像任何其他抽象类型一样通过接口使用。 The aim is to give a value-oriented semantics that remains consistent in the presence of statefulness. 目的是提供一种在有状态存在的情况下保持一致的价值导向语义。

  • In Mercury, at least (I can't speak for Clean), uniqueness is a more limited concept than ownership, in that there must be exactly one reference. 在水星中,至少(我不能说清洁),唯一性是一个比所有权更有限的概念,因为必须有一个参考。 You can't share several copies of a reference on the proviso that they will not be written to, as can be done in Rust. 你不能分享一些参考文献的副本,条件是它们不会被写入,就像在Rust中可以做到的那样。 You also can't lend a reference for writing but get it back later after the borrower has finished with it. 您也不能提供写作参考,但在借款人完成后再将其取回。

  • Declaring something unique in Mercury does not guarantee that writing to references will occur, just that the compiler will check that it would be safe to do so; 在Mercury中声明一些独特的东西并不能保证写入引用会发生,只是编译器会检查它是否安全; it is still valid for an implementation to copy the contents of a unique reference rather than update in place. 它仍然适用于复制唯一引用的内容而不是更新到位的实现。 The compiler will arrange for the update in place if it deems it appropriate at its given optimization level. 如果认为在给定的优化级别上认为合适,编译器将安排适当的更新。 Alternatively, authors of abstract types may perform similar (or sometimes drastically better) optimizations manually, safe in the knowledge that users will be forced to use the abstract type in a way that is consistent with them. 或者,抽象类型的作者可以手动执行类似(或者有时更好)的优化,安全地知道用户将被迫以与他们一致的方式使用抽象类型。 Ownership in Rust, on the other hand, is more directly connected to the memory model and gives stronger guarantees about behaviour. 另一方面,Rust中的所有权更直接地与内存模型相关联,并且对行为提供了更强的保证。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM