rustArray pointer example in Rust
An array pointer in Rust is a pointer that points to the first element of an array. This allows us to access the elements of the array without having to pass the entire array. An example of an array pointer in Rust is shown below:
let arr = [1, 2, 3, 4];
let ptr = &arr[0];
Here, ptr is a pointer to the first element of the array arr. We can then access the elements of the array using the pointer, as shown below:
println!("{}", *ptr); // prints 1
println!("{}", *(ptr + 1)); // prints 2
println!("{}", *(ptr + 2)); // prints 3
println!("{}", *(ptr + 3)); // prints 4
The output of the above code will be:
1
2
3
4
In the code, the * operator is used to dereference the pointer and access the value of the element it points to. The ptr + n expression is used to access the nth element of the array.
Helpful links
Related
- How to print pointer in Rust
- How to get size of pointer in Rust
- Null pointer in Rust
- How to cast pointer to usize in Rust
- Pointer to array element in Rust
- How to get address of pointer in Rust
- How to get pointer of object in Rust
- How to get pointer of struct in Rust
- How to get pointer to variable in Rust
- Example of pointer offset in Rust
More of Rust
- How to replace strings using Rust regex?
- How to use regex lookbehind in Rust?
- Regex example to match multiline string in Rust?
- How to use captures_iter with regex in Rust?
- How to borrow hashmap in Rust
- How to use regex to match a group in Rust?
- How to use regex to match a double quote in Rust?
- How to insert an element into a Rust HashMap if it does not already exist?
- How to convert a Rust slice to a fixed array?
- How to use regex lookahead in Rust?
See more codes...