Struct in Rust

Tram Ho

What programming language does not have Struct .

Struct is similar to Tuple , but differs in one place: Struct accesses data by field name, and Tuple is equal to index.

Defining a Struct and declaring an instance of that Struct is as simple as this:

Result:

Reading the code should be easy to understand, here, just note that a Struct that wants to print to the screen with println!() must have #[derive(Debug)] at the top and print using the println!("{:?}", user1) or println!("{:#?}", user1)

Ownership with Structure

Ok, now I will test the properties of Ownership for User , looking at User has 2 fields, username and email of type String , as I said in the previous post, a Struct that in its component contains type String or Vector , the value will be stored on the Heap , I don’t know if it will be saved in the type of active sign_in_count stored on the Stack , email username stored on the Heap or saved all active sign_in_count username email on the Heap , but that’s it. There is also no need to think carefully, because the behavior of the User in these two cases is the same.

Consider for example:

Since the value of user1 is stored on the Heap , when assigning let user2 = user1 the ownership of that value has been transferred from user1 to user2 so println!("{:#?}", user1); will error.

If we can’t assign it, we use clone :

Result

Note: a Struct that wants to use clone must declare #[derive(Clone)] for that Struct

Let’s look at an example with a simple struct containing only fields of known data types:

Easy to understand, because Location only contains 2 value fields stored on the Stack , so the assignment does not affect the ownership , only a Struct who wants to perform the assignment must declare #[derive(Clone, Copy)] , Note that having Copy is a must have Clone , having Clone is not necessarily having Copy , if there is Copy without Clone , the following error will be displayed when compiling:

So if you want Location to behave like User , how to do it, simply remove Copy , now the assignment will change ownership , if you want to clone() , you must have Clone :

So why don’t we add Copy for the User struct above so that it can be assigned without affecting ownership , forbidden, Rust strictly forbids adding Copy to any struct that has at least 1 field with a data type of zero size. known in advance – the value stored on the Heap .

Create an instance from another instance

Here are the ways to create, the code is also easy to understand:

 

Unit-like structure

We can define a struct with no fields:

Methods and Associated Functions

The definition of accociated functions is as simple as this:

It’s also easy to understand how to define and use ha.

The method definition is as follows:

Also split into 2 impl as follows:

Looks simple enough to understand, just notice that here we are not declaring Copy for Rectangle , so it is imperative that the functions in impl Retangle we have to use reference & to ensure ownership for the variables.

Because Rectangle only has 2 fields of data type i32 , we can declare Copy for it, now the functions in impl Retangle do not need to use reference & anymore and still preserve the ownership of the variables.

Share the news now

Source : Viblo