Arbitrum Stylus logo

Stylus by Example

Arrays

Like solidity, Array can have a compile-time fixed size or a dynamic size. Here, we will guide you on how to use arrays in Stylus, we provide 3 examples here:

  • Dynamic-size array
  • Fixed-size array
  • Custom struct element array

Dynamic-size array

Dynamic-size array is an array that can change its size at runtime.

Declare a dynamic-size array in Stylus is similar to Solidity, you can use the following code to declare a dynamic-size array:

1sol_storage! {
2    #[entrypoint]
3    pub struct Arrays {
4        uint256[] arr;
5    }
6}
1sol_storage! {
2    #[entrypoint]
3    pub struct Arrays {
4        uint256[] arr;
5    }
6}

Then you can use the following code to push/get an element of the array and get the length of the array:

1#[public]
2impl Arrays {
3    // dynamic array
4    // push an element to the dynamic array
5    pub fn push(&mut self, i: U256) {
6        self.arr.push(i);
7    }
8
9    // get the element at the index
10    pub fn get_element(&self, index: U256) -> U256 {
11        self.arr.get(index).unwrap()
12    }
13
14    // get the length of the array
15    pub fn get_arr_length(&self) -> U256 {
16        U256::from(self.arr.len())
17    }
18
19    // remove will not change the length of the array
20    pub fn remove(&mut self, index: U256) {
21        let mut last_element = self.arr.setter(index).unwrap();
22        last_element.erase()
23    }
24}
1#[public]
2impl Arrays {
3    // dynamic array
4    // push an element to the dynamic array
5    pub fn push(&mut self, i: U256) {
6        self.arr.push(i);
7    }
8
9    // get the element at the index
10    pub fn get_element(&self, index: U256) -> U256 {
11        self.arr.get(index).unwrap()
12    }
13
14    // get the length of the array
15    pub fn get_arr_length(&self) -> U256 {
16        U256::from(self.arr.len())
17    }
18
19    // remove will not change the length of the array
20    pub fn remove(&mut self, index: U256) {
21        let mut last_element = self.arr.setter(index).unwrap();
22        last_element.erase()
23    }
24}

Fixed-size array

Fixed-size array is an array that has a fixed size at compile time.

Declare a fixed-size array in Stylus is similar to Solidity, you can use the following code to declare a fixed-size array:

1sol_storage! {
2    #[entrypoint]
3    pub struct Arrays {
4        uint256[3] arr2;
5    }
6}
1sol_storage! {
2    #[entrypoint]
3    pub struct Arrays {
4        uint256[3] arr2;
5    }
6}

Then you can use the following code to get an element of the array and get the length of the array:

1#[public]
2impl Arrays {
3    // fixed length array
4    // get an element from the fixed length array
5    pub fn get_arr2_element(&self, index: U256) -> U256 {
6        self.arr2.get(index).unwrap()
7    }
8
9    // get the fixed length array size
10    pub fn get_arr2_length(&self) -> U256 {
11        U256::from(self.arr2.len())
12    }
13
14    // set an element in the fixed length array
15    pub fn set_arr2_value(&mut self, index: U256, value: U256) {
16        self.arr2.setter(index).unwrap().set(value);
17    }
18}
1#[public]
2impl Arrays {
3    // fixed length array
4    // get an element from the fixed length array
5    pub fn get_arr2_element(&self, index: U256) -> U256 {
6        self.arr2.get(index).unwrap()
7    }
8
9    // get the fixed length array size
10    pub fn get_arr2_length(&self) -> U256 {
11        U256::from(self.arr2.len())
12    }
13
14    // set an element in the fixed length array
15    pub fn set_arr2_value(&mut self, index: U256, value: U256) {
16        self.arr2.setter(index).unwrap().set(value);
17    }
18}
  • Note that the fixed-size array can't push new elements.

Custom struct element array

Custom struct element array is an array that can store custom struct elements.

You can use the following code to declare a custom struct element array:

1sol_storage! {
2    #[entrypoint]
3    pub struct Arrays {
4        Info[] arr3; // struct array
5    }
6
7    pub struct Info {
8        address setter;
9        uint256 value;
10    }
11}
1sol_storage! {
2    #[entrypoint]
3    pub struct Arrays {
4        Info[] arr3; // struct array
5    }
6
7    pub struct Info {
8        address setter;
9        uint256 value;
10    }
11}

Then you can use the following code to push/get an element of the array and get the length of the array:

1#[public]
2impl Arrays {
3    // struct array
4    // push an element to the struct array
5    pub fn push_arr3_info(&mut self, value: U256) {
6        let msg_sender = self.vm().msg_sender();
7        let mut new_info = self.arr3.grow();
8        new_info.setter.set(msg_sender);
9        new_info.value.set(value);
10    }
11
12    // get the length of the struct array
13    pub fn get_arr3_length(&self) -> U256 {
14        U256::from(self.arr3.len())
15    }
16
17    // get the value of the struct array at the index
18    pub fn get_arr3_info(&self, index: U256) -> (Address, U256) {
19        let info = self.arr3.get(index).unwrap();
20        (info.setter.get(), info.value.get())
21    }
22
23    // Find the first index of the expected value in the array
24    pub fn find_arr3_first_expected_value(&self, expected_value: U256) -> U256 {
25        for i in 0..self.arr3.len() {
26            let (_, value) = self.get_arr3_info(U256::from(i));
27            if value == expected_value {
28                return U256::from(i);
29            }
30        }
31        // if not found, return the size of arr
32        U256::from(self.arr3.len())
33    }
34}
1#[public]
2impl Arrays {
3    // struct array
4    // push an element to the struct array
5    pub fn push_arr3_info(&mut self, value: U256) {
6        let msg_sender = self.vm().msg_sender();
7        let mut new_info = self.arr3.grow();
8        new_info.setter.set(msg_sender);
9        new_info.value.set(value);
10    }
11
12    // get the length of the struct array
13    pub fn get_arr3_length(&self) -> U256 {
14        U256::from(self.arr3.len())
15    }
16
17    // get the value of the struct array at the index
18    pub fn get_arr3_info(&self, index: U256) -> (Address, U256) {
19        let info = self.arr3.get(index).unwrap();
20        (info.setter.get(), info.value.get())
21    }
22
23    // Find the first index of the expected value in the array
24    pub fn find_arr3_first_expected_value(&self, expected_value: U256) -> U256 {
25        for i in 0..self.arr3.len() {
26            let (_, value) = self.get_arr3_info(U256::from(i));
27            if value == expected_value {
28                return U256::from(i);
29            }
30        }
31        // if not found, return the size of arr
32        U256::from(self.arr3.len())
33    }
34}

Full example code:

src/lib.rs

1Loading...
1Loading...

Cargo.toml

1Loading...
1Loading...