Array of strings - access element by index
-
mut
-
To allow us to change the values in the array we have to make it mutable using
mut
. -
In most cases there is no need to decalre the type of the values and the length. Rust can infer.
fn main() { let mut animals = ["cat", "snake", "fish"]; println!("{animals:?}"); println!("{}", animals[1]); animals[1] = "crab"; println!("{animals:?}"); }
["cat", "snake", "fish"]
snake
["cat", "crab", "fish"]