import "vec"
This structure is a self-expanding list of elements. You'll not need to allocate memory yourself in order to add more values.
Instantiate a new std::Vec<T>
.
fn new() : std::Vec<T>
A new instance of std::Vec<T>
.
let vec = std::Vec<i32>::new();
The current size of the vector. This represents how much values are actually inside the vector.
u64
let vec = std::Vec<i32>::new();
vec.push(5);
vec.push(18);
vec.push(2);
vec.length; // 3
Push a value inside the vector. The value must be of type T
or convertible to T
.
fn push(value: const T&) : u64
The updated size of the vector.
let vec = std::Vec<i32>::new();
vec.push(1);
vec.push(2);
vec.push(3);
Remove and return the first value of the vector.
fn shift() : T
The first value of the vector (which should be removed from the vector).
let vec = std::Vec<i32>::new();
vec.push(5);
vec.push(18);
vec.push(2);
vec.shift(); // 5
vec.shift(); // 18
vec.length; // 1
fn pop() : T
Remove and return the last value of the vector.
The last value of the vector (which should be removed from the vector).
let vec = std::Vec<i32>::new();
vec.push(5);
vec.push(18);
vec.push(2);
vec.pop(); // 2
vec.pop(); // 18
vec.length; // 1
fn filter(cb: fn (T&): bool) : std::Vec<T>
Create a new std::Vec<T>
with only values that returns true
when passing in the callback.
A vector consisting of filtered values of the same type as the original vector.
let vec = std::Vec<i32>::new();
vec.push(3);
vec.push(4);
vec.push(5);
let odds = vec.filter(fn (value: i32&) : bool {
return (value % 2) == 1;
});
odds.length; // 2
odds[0]; // 3
odds[1]; // 5
let vec = std::Vec<i8*>::new();
vec.push("hi");
vec.push("no");
vec.push("nutshell");
vec.push("squirrel");
vec.push("nature");
let words = vec.filter(fn (value: i8*&) : bool {
return value[0] == 'n';
});
words.length; // 3
words[0]; // no
words[1]; // nutshell
words[2]; // nature
fn reduce<U>(cb: fn (acc: U&, value: const T&) : U, default: U) : U
Accumulate all values into a single value.
A value of type U
.
let vec = std::Vec<i32>::new();
vec.push(1);
vec.push(2);
vec.push(3);
let sum = vec.reduce<i32>(fn (acc: i32&, value: const i32&) : i32 {
return acc += value;
}, 0);
sum; // 6
let vec = std::Vec<i32>::new();
vec.push(1);
vec.push(2);
vec.push(3);
let sum = vec.reduce<i32>(fn (acc: i32&, value: const i32&) : i32 {
if value % 2 == 1 {
acc += value;
}
return acc;
}, 0);
sum; // 4
let vec = std::Vec<i32>::new();
vec.push(8);
vec.push(4);
vec.push(6);
let even = vec.reduce<bool>(fn (acc: bool&, value: const i32&) : bool {
return acc && (value % 2 == 0);
}, true);
even; // true
fn is_even(acc: bool&, value: const i32&) : bool {
return acc && (value % 2 == 0);
}
fn main() {
let vec = std::Vec<i32>::new();
vec.push(8);
vec.push(4);
vec.push(6);
let even = vec.reduce<bool>(is_even, true); // true
}
fn map<U>(cb: fn (value: T, index: u64, original: const std::Vec<T>&) : U) : std::Vec<U>
Transform each values of a vector to the function returned values. The function will pass each values of a vector one by one to the callback function. It lets the original vector unmodified.
A new vector of type U
which should be specified in the function's call.
import "string"
fn main() {
let vec = std::Vec<i32>::new();
vec.push(3);
vec.push(4);
vec.push(5);
let strings = vec.map<i8*>(fn (value: i32, index: u64, original: const std::Vec<i32>&) : std::Vec<i8*> {
return std::to_string(value);
});
strings[0]; // 3
strings[1]; // 4
strings[2]; // 5
}
let vec = std::Vec<i32>::new();
vec.push(3);
vec.push(4);
vec.push(5);
let multiplied_by_2 = vec.map<i32>(fn (value: i32, index: u64, original: const std::Vec<i32>&) : std::Vec<i32> {
return value * 2;
});
multiplied_by_2[0]; // 6
multiplied_by_2[1]; // 8
multiplied_by_2[2]; // 10
Get a value by its index.
fn get(index: const i64&) : T&
A non-const reference to the element being at the given index.
let vec = std::Vec<i32>::new();
vec.push(1);
vec.push(2);
vec.push(3);
vec.get(0); // 1
vec.get(2); // 3
Operator overloading of []
that gets a value by its index.
fn [](value: const i64&) : T&
A non-const reference to the element being at the given index.
let vec = std::Vec<i32>::new();
vec.push(1);
vec.push(2);
vec.push(3);
vec[0]; // 1
vec[2]; // 3