Introduction
We create struct instances to store related data together in one place. It helps organize information clearly.
We create struct instances to store related data together in one place. It helps organize information clearly.
struct StructName {
field1: Type1,
field2: Type2,
// more fields
}
let instance_name = StructName {
field1: value1,
field2: value2,
// more fields with values
};Use the struct name followed by curly braces to create an instance.
Each field must be given a value when creating the instance.
Person instance named person1 with name "Alice" and age 30.struct Person {
name: String,
age: u8,
}
fn main() {
let person1 = Person {
name: String::from("Alice"),
age: 30,
};
}Point instance called origin at coordinates (0, 0).struct Point {
x: i32,
y: i32,
}
fn main() {
let origin = Point { x: 0, y: 0 };
}This program creates a Book struct instance and prints its details.
struct Book {
title: String,
author: String,
pages: u32,
}
fn main() {
let my_book = Book {
title: String::from("Rust Basics"),
author: String::from("Jane Doe"),
pages: 250,
};
println!("Book: {} by {} has {} pages.", my_book.title, my_book.author, my_book.pages);
}All fields must be initialized when creating an instance unless you use default values.
Use String::from to create string values for fields of type String.
Struct instances group related data into one object.
Create instances by naming the struct and giving values for each field.
Instances let you work with organized data easily.
Person with fields name and age in Rust?Person { name: String::from("Alice"), age: 30 }, which is the correct way. Other options use incorrect syntax like tuple style or assignment inside parentheses.Car with fields make and year?struct Point {
x: i32,
y: i32,
}
fn main() {
let p = Point { x: 5, y: 10 };
println!("({}, {})", p.x, p.y);
}Point has fields x and y. The instance p assigns x = 5 and y = 10.println! macro prints the values of p.x and p.y in order, so output is (5, 10).struct Book {
title: String,
pages: u32,
}
fn main() {
let b = Book { title: "Rust Book", pages: 300 };
}title is of type String, but the code assigns a string literal "Rust Book" directly, which is a "str" type, causing a type mismatch.pages is correctly assigned an integer. The struct definition and instance have correct syntax otherwise.Employee with fields name: String, id: u32, and active: bool. How do you create a new instance with name as "John", id as 101, and active as true, using shorthand for the active field if you have a variable active set to true?field: value.active variable is used directly as a field. let active = true;
let e = Employee { name: "John", id: 101, active: active }; uses explicit active: active but has type mismatch on name. let e = Employee { name: String::from("John"), id: 101, active: true }; does not use the variable. let active = true;
let e = Employee { name: String::from("John"), id: 101, active = active }; uses invalid syntax with = inside struct initialization.Pass the test and this pattern moves unknown