The Rust Programming Language — Collections — Structs
Structs in Rust are kind of a mixture of an interface and a class from TypeScript/JavaScript. Structs, like tuple, cannot be altered. We cannot change the properties of the object during the lifecycle of the program.
Structs can be defined using struct keyword in Rust. Unlike tuples, which are index based, we have properties of an object in a struct and hence they are based on the name of the properties in an object. Have a look at the below code snippet:
struct Point {
x: i64,
y: i64,
z: i64
}Observations:
- As you can see, you need to use
structkeyword to define a struct. - You need to give a name, in our case
Point. - You can define the properties inside the struct, i.e.
x,yandz. - You can mention the types of each and every property, i.e.
i64.
When declaring a struct, you need to mention the name of the struct you are declaring. See the below code snippet:
struct Point {
x: i64,
y: i64,
z: i64
}
fn main() {
let point: Point = Point { x: 0, y: 1, z: 2 };
let point1: Point = new_point(3, 4, 5);
let point2: Point = new_point2(6, 7, 8);
println!("Point x: {}", point.x);
println!("Point y: {}", point.y);
println!("Point z: {}", point.z);
println!("Point x: {}", point1.x);
println!("Point y: {}", point1.y);
println!("Point z: {}", point1.z);
println!("Point x: {}", point2.x);
println!("Point y: {}", point2.y);
println!("Point z: {}", point2.z);
}
fn new_point(a: i64, b: i64, c: i64) -> Point {
return Point { x: a, y: b, z: c };
}
fn new_point2(x: i64, y: i64, z: i64) -> Point {
Point { x, y, z }
}Observations:
- When assigning value to
point, we are using name of the structPointbefore assigning the value.let point: Point = Point { x: 0, y: 1, z: 2 }; - Return type of functions
new_pointandnew_point2isPoint. new_pointreturns via statement convention where asnew_point2returns via expression as a last line of the function.- We need to mention the name, i.e.
Point, of the struct before returning.
Just like tuples, you can de-structure the struct. Look at the below code snippet:
struct Point {
x: i64,
y: i64,
z: i64
}
fn main() {
let point: Point = Point { x: 0, y: 1, z: 2 };
let point1: Point = new_point(3, 4, 5);
let point2: Point = new_point2(6, 7, 8);
let Point {x, y, z} = point;
let Point {x, y: _, z} = point1;
let Point {x, ..} = point2;
}
fn new_point(a: i64, b: i64, c: i64) -> Point {
return Point { x: a, y: b, z: c };
}
fn new_point2(x: i64, y: i64, z: i64) -> Point {
Point { x, y, z }
}Observations:
- Ignoring a property while de-structuring, you need to use
_against the property name you wish to ignore. - Ignoring all the other properties can be done using
..and you can focus on only the properties you are interested.
Like tuples, structs are also mutable using mut keyword. Refer to below code snippet:
struct Point {
x: i64,
y: i64,
z: i64
}
fn main() {
let mut point: Point = Point { x: 0, y: 1, z: 2 };
point.x = point.x + 1;
println!("Point x: {}", point.x);
}To re-iterate, struct cannot be modified in structure while the code is running. You can make nested tuples and nested structs and tuples can contain structs and structs can contain tuples.
That’s it for structs in The Rust Programming Language. Please share your feedback in the comments section.
You can subscribe to my newsletter about The Rust Programming Language here. You can read about all the articles in this series here.






