To find the first element in a Rust array, you can access the element at index 0 using square brackets []
notation.
Here's an example:
main.rs102 chars4 lines
This code initializes an array with three elements [1, 2, 3]
. Then, it assigns the first element of the array (1
) to the first_element
variable, and prints out the value. The output will be:
main.rs24 chars2 lines
If you want to find the first element of an array that is mutable (&mut
), you can use the same syntax to mutate the element:
main.rs87 chars4 lines
This code initializes a mutable array with three elements [1, 2, 3]
. Then, it mutates the first element to be 4
, and prints out the new value of the first element. The output will be:
main.rs28 chars2 lines
gistlibby LogSnag