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

How it works...

Sequence types are compound types that allocate a continuous part of the memory for faster and easier access. Vec<T> creates a simple, heap-allocated version of an array that grows (and shrinks) dynamically (step 4). 

The original array (step 2) allocates memory on the stack and has to have a known size at compile time, which is a significant factor in using it. Both can be iterated and viewed using slices (https://doc.rust-lang.org/book/ch04-03-slices.html).

Tuples (step 3) are a different beast since they don't lend themselves to slices and are more a group of variables that have a semantic relationship—like a point in a two-dimensional space. Another use case is to return more than one variable to the caller of a function without the use of an additional struct or misusing a collection type.

Sequences in Rust are special because of the low overhead they produce. The size of Vec<T> is a pointer to an n * size of T memory on the heap, along with the size of the allocated memory, and how much of that is used. For arrays, the capacity is the current size (which the compiler can fill in during compilation), and tuples are more or less syntactic sugar on top of three distinct variables. Each of the three types provides convenience functions to change the contents—and, in the case of Vec<T>, the size of the collection. We recommend taking a close look at the tests and their comments to find out more about each type.  

We have covered the basics of sequences in Rust, so let's move on to the next recipe.

主站蜘蛛池模板: 汉寿县| 蒲江县| 乐至县| 云阳县| 太仓市| 宽甸| 沙洋县| 化隆| 灌阳县| 屯留县| 乐安县| 自贡市| 水城县| 黔江区| 武义县| 南岸区| 陕西省| 大安市| 金乡县| 陆良县| 彭阳县| 安福县| 固始县| 临沧市| 萨迦县| 蓝田县| 宁强县| 巩留县| 德清县| 霍山县| 白山市| 邮箱| 邢台县| 武功县| 桂阳县| 六安市| 同心县| 寿宁县| 南涧| 年辖:市辖区| 奉新县|