A Guide to Working With Rust Structs

One key feature that makes Rust powerful and more appealing is its support for structs. Structs are composite data types for grouping related data. They allow you to define custom data types with named fields that improve code organization and make data manipulation easier.

you could use structs to represent various objects, including user-defined data types and configurations. Structs are more flexible than many other data structures. Their ability to encapsulate related data makes them useful since they create a more logical model of real-world objects.

4

Defining Structs and Struct Fields

You’ll define a struct by using thestructkeyword followed by a name for the struct. Follow this by specifying the struct’s field names and theirRust data typesinside a pair of curly braces.

ThisPersonstruct has three fields. Thenamefield is a string, theagefield is an unsigned 8-bit integer, and theis_studentfield is a boolean.

Rust programming language logo

it’s possible to use struct literals to create struct instances after the struct definition. Struct literals specify the values of a struct’s fields.

Thepersonvariable is an instance of thePersonstruct, created with a struct literal. This literal instantiates all the fields of the struct with values of their corresponding data type.

Result of Accessing and Modifying Struct Fields

Using Constructors to Create Structs

You can also use a constructor function to create an instance of a struct.

The program above defines a constructor for thePersonstruct with theimplkeyword. Thenewconstructor takes in the struct fields as arguments and returns a new instance of the struct with the initialized values.

Result of Declaring Methods for Structs

You can create aPersoninstance with thenewconstructor, passing it appropriate arguments.

Accessing and Modifying Struct Fields

You can access and modify struct fields using a dot notation. You simply use the dot (.) operator followed by the field’s name to access or change its value.

The program creates a struct, instantiates the struct, prints thename,andagefields, and modifies theis_studentfield before printing the field.

Rust programming language logo

Declaring Methods for Structs

Unlikestructs in C, you can define methods on Rust structs that operate on an instance. Methods are functions that take a reference to a struct for access and modification. You can also use the dot notation to call the methods of a struct and access their functionality.

Here’s how you can declare methods for structs and use those methods to carry out operations:

The program defines two methods for thePersonstruct. Thesay_hellomethod takes a reference toselfand prints a greeting that includes the person’s name and age. Thehave_birthdaymethod takes a mutable reference toselfand increments the person’s age.

Rust’s Ownership Model Optimizes Memory Management

Structs are versatile data structures, acting as a sort of minimal class equivalent.

As with other Rust data structures, you should follow Rust’s ownership rules when working with struct variables. The ownership model ensures that you manage memory efficiently in your programs, preventing common issues like null and dangling pointers.

Rust is a powerful language, with some complicated and unique behavior. Ownership is a concept that is vital to understand correctly.

Who asked for these upgrades?

You’ve been quoting these famous films wrong all along!

you’re able to’t call this offline, Notion.

So much time invested, and for what?

Taming data is easier than it looks.

Technology Explained

PC & Mobile