rustExample of array of structs in Rust
An array of structs in Rust is a collection of structs stored in a contiguous memory location. Structs are custom data types that allow you to store multiple values of different types in a single variable. An array of structs is declared using the [struct; size] syntax, where struct is the struct type and size is the number of elements in the array.
Example code
struct Point {
x: i32,
y: i32,
}
let points = [Point { x: 0, y: 0 }; 5];
Output example
[
Point { x: 0, y: 0 },
Point { x: 0, y: 0 },
Point { x: 0, y: 0 },
Point { x: 0, y: 0 },
Point { x: 0, y: 0 },
]
Code explanation
struct Point { x: i32, y: i32, }: This declares a struct type calledPointwith two fields,xandy, both of typei32.let points = [Point { x: 0, y: 0 }; 5];: This declares an array ofPointstructs with 5 elements, each withxandyfields set to0.
Helpful links
Related
- How to join structs in Rust
- Example of bit field in Rust struct
- Example of struct private field in Rust
- Example of Rust struct with closure
- How to convert struct to protobuf in Rust
- Example of constant struct in Rust
- How to update struct in Rust
- How to extend struct from another struct in Rust
- How to copy struct in Rust
- Example of struct with vector field in Rust
More of Rust
- Regex example to match multiline string in Rust?
- How to match the end of a line in a Rust regex?
- How to create a HashMap of structs in Rust?
- How to join two Rust HashMaps?
- How to replace a capture group using Rust regex?
- How to modify an existing entry in a Rust HashMap?
- How to use non-capturing groups in Rust regex?
- How to use regex lookahead in Rust?
- How to use regex captures in Rust?
- How to extend struct from another struct in Rust
See more codes...