Home
Map
Initialize ArrayUse the vec, array and slice syntax to create arrays and slices and initialize them with values.
Rust
This page was last reviewed on Jun 28, 2023.
Initialize array. In Rust programs we have arrays, slices, and vectors and they often can be used interchangeably. We can initialize them with a variety of syntax forms.
String Array
Shows an array
With an array or vector, if we take a reference, we can get a slice. The slice can be used as a type in function arguments for more versatile code.
Syntax examples. This example initializes vectors, slices and arrays in a variety of ways. For slices, we have to create a vector or array and take a reference to part of it.
Tip For a String vector, make sure to use to_string(). String literals are "str" references, not Strings.
Also For arrays, we specify a value and the number of repetitions in square brackets.
Finally For vectors, we can use methods like push() to append to a vector, or extend_from_slice which is faster.
vec
push u8 Vec
extend from slice
Shows an array
fn main() { // Vector. let animals = vec!["bird", "frog", "dog"]; println!("{:?}", animals); // Vector. let animals = vec!["bird".to_string(), "frog".to_string()]; println!("{:?}", animals); // Array. let animals = ["bird", "frog", "dog"]; println!("{:?}", animals); // Slice of array. let animals_slice = &animals[0..2]; println!("{:?}", animals_slice); // Vector. let mut animals = Vec::with_capacity(2); animals.push("bird"); animals.push("frog"); animals.push("dog"); println!("{:?}", animals); // Slice from vector. let animals_slice = animals.as_slice(); println!("{:?}", animals_slice); // Array. let numbers = [0; 10]; println!("{:?}", numbers); // Array. let numbers: [u8; 5] = [1; 5]; println!("{:?}", numbers); // Array. let numbers = [1, 2, 3, 4, 5, 6, 7]; println!("{:?}", numbers); // Slice. let numbers_slice = &numbers[0..2]; println!("{:?}", numbers_slice); // Slice. let numbers_slice = &numbers[..2]; println!("{:?}", numbers_slice); // Slice. let numbers_slice = &numbers[4..]; println!("{:?}", numbers_slice); // Slice. if let Some(numbers_slice) = numbers.get(1..3) { println!("{:?}", numbers_slice); } // Vector. let mut numbers = vec![]; let numbers_slice = [10, 20, 30]; numbers.extend_from_slice(&numbers_slice); println!("{:?}", numbers); }
["bird", "frog", "dog"] ["bird", "frog"] ["bird", "frog", "dog"] ["bird", "frog"] ["bird", "frog", "dog"] ["bird", "frog", "dog"] [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [1, 1, 1, 1, 1] [1, 2, 3, 4, 5, 6, 7] [1, 2] [1, 2] [5, 6, 7] [2, 3] [10, 20, 30]
An important detail. After reviewing these initialization examples, it becomes clear that slices are "views" of existing data like arrays or vectors.
And Slices are references to existing data. To store data in a struct, we should use Vectors or arrays—not slices.
Also In Rust we can use slices of Strings as substrings. This is efficient and similar to how slices work on Vectors and arrays.
Substring
Get. The get() function is similar to taking a slice, but it returns an Option. To deal with the Option in the clearest way, we can use the if let Some syntax.
if
Summary. In Rust we can allocate vectors and arrays. For vectors, we often use the "vec" macro. And slices can take references to ranges of vectors and arrays in a unified way.
C#VB.NETPythonGoJavaSwiftRust
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jun 28, 2023 (edit).
Home
Changes
© 2007-2023 Sam Allen.