$$\text{只有心灵的淡定宁静,继而产生的身心愉悦,才是幸福的真正源泉}$$
软件篋borrowing_exerci(一)
借用和所有权机制的基本思想很简单。每个值都由一个对象拥有,在一个时刻只能由一个对象拥有,并且当对象超出范围时,该值将从内存中删除。只能借用该对象,而不能拥有该对象的所有权,但是可以创建引用对象拥有该对象值的指针,引用对象拥有该对象值的所有权,但是该对象可以随时收回其所有权。
软件篋borrowing_exerci
Rust语言的借用机制是其最主要的特点。通过实现借用方法,使得代码更加安全和可靠。
Rust语言存在两大数据类型:固定大小类型和可变大小类型。本章重点说明固定大小类型的借用机制。
固定大小类型
固定大小类型也称之为静态类型、常规固定大小类型或者栈分配值的类型。在编译时知道这种类型的大小,在编译以后,它们大小不可增长或缩小。比如,固定数组是元素的固定大小的列表。
可变大小类型
可变大小类型也称之为动态类型、可变大小的容器或者堆分配值的类型。在编译时不知道其类型的大小,但是它们可以随时增长或缩小其容量大小。比如,向量是可调整大小的数组,一种连续的可增长数组类型。
参考资料
- split-a-module-across-several-files
- ch07-05-separating-modules-into-different-files
- splitting-up-modules-in-rust
- references-and-borrowing
- rust-by-example/scope/borrow
- rust-borrowing-and-ownership
- understanding-rust-ownership-borrowing-lifetimes
- you-can-t-turn-off-the-borrow-checker-in-rust
- cant-derive-copy-because-of-string
- whats-the-difference-between-trait-copy-and-clone
- what-is-the-difference-between-copy-and-clone
- rust-move-copy
- rust-crash-course-02-basics-of-ownership
- move-clone-copy
- the-basics-of-rust-structs
- a-single-command-to-compile-and-run-rust-programs
- how-to-execute-rust-code-directly-on-unix-systems-using-the-shebang
- rust-ownership