vector<T> is the only primitive collection type provided by Move. A
vector<T> is a homogenous collection of
T's that can grow or shrink by pushing/popping values off the "end".
vector<T> can be instantiated with any type
T, including resource types and other vectors. For example,
vector<vector<u8>> are all valid vector types.
vector supports the following operations via the
0x1::Vector module in the Move standard library:
|Create an empty vector that can store values of type ||Never|
|Create a vector of size 1 containing ||Never|
|Remove and return the last element in ||If |
|Return an immutable reference to the ||If |
|Return an mutable reference to the ||If |
|Delete ||If |
|Add the elements in ||If |
More operations may be added overtime
Destroying and copying
Some behaviors of
vector<T> depend on whether
T is a resource type. For example, vectors containing resources cannot be implictly discarded like
v in the example above--they must be explicitly destroyed with
Vector::destroy_empty will abort at runtime unless
vec contains zero elements.
This error would also happen for an unconstrained
T as the type might be a resource.
But no error would occur for dropping a vector that contains
Similarly, resource vectors cannot be copied. A
vector<T> is copyable if and only if
T is copyable. However, even copyable vectors are never implicitly copied:
Copies of large vectors can be expensive, so the compiler requires explicit
copy's to make it easier to see where they are happening.
A common use-case for vectors in Move is to represent "byte arrays", which are represented with
vector<u8>. These values are often used for cryptographic purposes, such as a public key or a hash result.
There are currently two supported types of
vector<u8> literals, byte strings and hex strings.
Byte strings are quoted string literals prefixed by a
These are ASCII encoded strings that allow for escape sequences. Currently, the supported escape sequences are
|New line (or Line feed)|
|Hex escape, inserts the hex byte sequence |
Hex strings are quoted string literals prefixed by a
Each byte pair, ranging from
FF, is interpreted as hex encoded
u8 value. So each byte pair corresponds to a single entry in the resulting
Currently, there is no support for general
vector<T> literals in Move where
T is not a
u8. However, Move bytecode supports
vector<T> constants for any primitive type
T. We plan to add
vector<T> literals to the source language that can compile to bytecode constants.