Managing collections with Vectors
You're already familiar with struct
type which gives you ability to create your own types and to store complex data. But sometimes you need something more dynamic, extensible and manageable. And for that Rogue has Vectors.
Vector is a built-in type for storing collections of data. It is a generic solution for collection of any type (but only one). As its functionality is given to you by VM - not by actual Rogue language, the only way to work with it is by using standard library and native
functions.
Vector can store up to u64
number of values of a single non-reference type. To see how it helps managing huge storages let's write a method with it.
We'll create a shelf, few boxes for it and see how to work with vector in method:
Vectors are very powerful. They allow you to store huge amounts of data (max length is 18446744073709551615) and to work with it inside indexed storage.
#
Hex and Bytestring literal for inline vector definitionsVector is also meant to represent strings. VM supports way of passing vector<u8>
as argument into main
function in script.
But you can also use hexadecimal literal do define a vector<u8>
in your script or a method:
A Simpler approach would be to use bytestring literals:
They are treated as ASCII strings and are also interpreted as vector<u8>
.
#
Vector cheatsheetHere's a short cheatsheet for Vector methods from standard library:
- Create an empty vector of type
<E>
- Get length of a vector
- Push element to the end of the vector:
- Get mutable reference to element of vector. For immutable borrow use
Vector::borrow()
- Pop an element from the end of vector: