This is the 28th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
Base code Sample
To understand when to use a structure, the official documentation gives an example of calculating the area of a rectangle, where we refactor the code step by step until we use a structure.
The specific logic to calculate the area of the rectangle is to obtain the width and height of the rectangle, and then calculate the area of the rectangle through the formula, as shown in the following code example:
fn main() {
let width = 10;
let height = 20;
println!("The area of a rectangle is {}", area(width, height))
}
fn area(width: u32, height: u32) - >u32{
width * height
}
Copy the code
Running the program through Cargo Run yields the following result:
The area of the rectangle is 200Copy the code
Although the above code can be run and the area of a rectangle can be calculated by passing in the length and width of the rectangle when calling area, the width and height are related. This problem is particularly apparent in the area function signature. Area can calculate the area of a rectangle, but it takes two parameters. These two parameters should be related to each other, but the program itself does not show this, and it would be easier to understand and deal with if you could combine the length and width together. So we can use tuples for code refactoring.
fn area(width: u32, height: u32) - >u32
Copy the code
Refactoring code using tuples
The following is an example of code for a tuple refactoring:
fn main() {
let tup_w_h = (10.20); // Index 0 corresponds to width
println!("The area of a rectangle is {}", area(tup_w_h))
}
fn area(tup_w_h: (u32.u32)) - >u32{
tup_w_h.0 * tup_w_h.1
}
Copy the code
The tuple version of the code is a bit better than the base code, helping us to add some structure, and the area only needs to be passed as a single parameter, but the code in this version is not very clear: Is a tuple does not give the name of the element, only through every part of the index for yuan group, although when calculating the area of confusion but it doesn’t matter if the width on the screen drawing would be a big problem, we must make clear which element in a tuple is long which is wide, so it is easy to confuse and cause errors, The reason is that the meaning of the data is not communicated in the code.
Use structure weight structure code
Now we can use structs to give meaning to data names. We can convert a tuple to a data type with a whole name and a name for each part – struct:
struct WidthHeight {
width: u32,
height: u32,}fn main() {
let tup_w_h = WidthHeight {width: 10, height: 20}; // Index 0 corresponds to width
println!("The area of a rectangle is {}", area(&tup_w_h))
}
fn area(width_height: &WidthHeight) -> u32{
width_height.height * width_height.width
}
Copy the code
The above example code defines the structure WidthHeight and the fields width and height, both of type U32, in curly braces. Then in Main, we create a concrete instance of WidthHeight.
The area function is now defined to take a parameter named width_height, whose type is an immutable borrow of an instance of the structure WidthHeight. As mentioned in the previous article, we want to borrow the structure rather than take ownership of it so that main retains ownership of tup_w_h and continues to use it, so that’s why there are ampersand where the function is signed and called.
The area function accesses the width and height fields of the WidthHeight instance. The area function signature now explicitly states our intent: calculate the area of WidthHeight using the width and height fields. This indicates that the width and height are related and provides descriptive names for these values rather than using tuples of index values 0 and 1. The structure is much clearer.
conclusion
The article was first published in the wechat public account Program Yuan Xiaozhuang, at the same time in nuggets.
The code word is not easy, reprint please explain the source, pass by the little friends of the lovely little finger point like and then go (╹▽╹)