Struct purp_value::types::array::Array
source · pub struct Array {
pub values: Vec<Value>,
}
Expand description
Represents an array of Value
s.
Fields§
§values: Vec<Value>
Implementations§
source§impl Array
impl Array
sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty Array
.
Examples
use my_crate::Array;
let empty_array = Array::new();
assert_eq!(empty_array.len(), 0);
sourcepub fn push(&mut self, value: Value)
pub fn push(&mut self, value: Value)
Appends a value to the end of the array.
Examples
use my_crate::{Array, Value};
let mut array = Array::new();
array.push(Value::from(42));
array.push(Value::from("hello"));
assert_eq!(array.len(), 2);
assert_eq!(array.get(0), Some(&Value::from(42)));
assert_eq!(array.get(1), Some(&Value::from("hello")));
sourcepub fn pop(&mut self) -> Option<Value>
pub fn pop(&mut self) -> Option<Value>
Removes the last element from the array and returns it, or None
if the array is empty.
Examples
use my_crate::{Array, Value};
let mut array = Array::new();
array.push(Value::from(42));
let popped_value = array.pop();
assert_eq!(popped_value, Some(Value::from(42)));
let empty_array = Array::new();
let empty_popped_value = empty_array.pop();
assert_eq!(empty_popped_value, None);
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the array.
Examples
use my_crate::{Array, Value};
let mut array = Array::new();
assert_eq!(array.len(), 0);
array.push(Value::from(42));
assert_eq!(array.len(), 1);
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if the array contains no elements.
Examples
use my_crate::{Array, Value};
let empty_array = Array::new();
assert!(empty_array.is_empty());
let mut array = Array::new();
array.push(Value::from(42));
assert!(!array.is_empty());