简体   繁体   English

Rust的借用规则是否妨碍了功能数据结构的发展?

[英]Do Rust's borrowing rules get in the way of functional data structures?

Functional data structures (such as the Hash Array Mapped Trie used in Haskell/Clojure/Scala) rely on lots of sharing in the underlying data structure. 功能数据结构(例如Haskell / Clojure / Scala中使用的Hash Array Mapped Trie)依赖于底层数据结构中的大量共享。 For example, if we implement insert on a map-like data type that's usually implemented by path-copying on the tree that implements the data structure. 例如,如果我们在类似地图的数据类型上实现insert ,那么通常通过在实现数据结构的树上进行路径复制来实现。

Given that these data structures rely a lot on sharing (and no principal owner of) underlying values, will borrowing get in the way of implementing such structures? 鉴于这些数据结构在很大程度上依赖于基础价值的共享(并且没有主要所有者),借款是否会妨碍实施此类结构?

Short Answer: No . 简答:

Long Answer: 答案很长:

Rust actually works very well with immutable structures (it gives more guarantees than C's const for example). Rust实际上对不可变结构非常有效(例如,它提供了比C的const更多的保证)。

The shared ownership is no problem ( Rc / Arc ) with a truly immutable value, and you can easily borrow multiple times into an immutable structure. 共享所有权没有问题( Rc / Arc )具有真正不可变的值,您可以轻松地多次借入不可变结构。 You cannot move while borrowing, but this can be circumvented by handing out owning proxies (via Rc or Arc once again) instead of references. 借用时你不能移动,但这可以通过分发拥有代理(再次通过RcArc )而不是引用来规避。

The one issue in Rust that you may not have in Haskell is mixing mutable values in with Cell or RefCell as you can then create cycles and those won't be collected because Rust has no GC. 您在Haskell中可能没有的Rust中的一个问题是将可变值与CellRefCell混合,然后您可以创建循环,因为Rust没有GC,所以不会收集这些循环。

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

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