官术网_书友最值得收藏!

Summary

Moving one level above the details of code, this chapter discusses considerations when designing and using types. Rust's differentiation between stack- and heap-allocated variables in code provides a level of control that should be used to improve performance and API flexibility. Sized, a marker trait for mostly stack-allocated values, is the default for generic type parameters and can be relaxed by applying the ?Sized constraint instead.

When working with more object-oriented architectures, trait objects become a way to "work with interfaces" instead of specific implementations. However, they come at a performance cost, that is, dynamic dispatch, another trade-off between maintainability and performance.

Other than moving, Rust can copy or clone variables when necessary. Copy performs a deep copy in the case of sized values; unsized values require a reference that has to be cloned instead. Using these operations is often encountered when working with immutable data types in a principle called copy-on-write. Choosing whether we are able to reason about the object's state at any given time and avoiding data race conditions, but having to create a copy for each change, is another important trade-off when designing data structures.

This trade-off will become apparent in the next chapter, where we will start working with lists, such as the singly-linked list, doubly-linked list, and the dynamic array.

主站蜘蛛池模板: 右玉县| 新宾| 上林县| 阜阳市| 建宁县| 沅江市| 扶沟县| 大安市| 广西| 来宾市| 南昌市| 汉沽区| 定结县| 昌乐县| 义乌市| 沁水县| 奉节县| 万安县| 专栏| 涿鹿县| 云浮市| 姜堰市| 德格县| 准格尔旗| 鹤庆县| 五峰| 佳木斯市| 太白县| 徐汇区| 永福县| 九龙城区| 常州市| 平顶山市| 沿河| 义马市| 金川县| 怀来县| 黄石市| 醴陵市| 广河县| 鸡西市|