Rust learning record - > generic usage: structs, methods, references, and properties

preface This paper analyzes a problem encountered by the author in learning the use of t rust generics and gives a solut...
1. Use immutable references
2. Declare generic properties

preface

This paper analyzes a problem encountered by the author in learning the use of t rust generics and gives a solution

Tip: the following is the main content of this article. The following cases can be used for reference

1, Problem description

First, create the following structure containing two undeclared features. The code is as follows:

struct str2<T,E> { x:T, y:E, }

Then declare the methods included:

impl<T,E> str2<T,E> { fn get_x(&self) -> &T { &self.x } fn get_y(&self) -> &E { &self.y } fn create<V, W> (self, other: str2<V,W>) -> str2<T,W> { str2 { x:self.x, y:other.y, } }

The problem lies in the call to the create method:
Create an instance as follows (all instances in the following text refer to here):

let a = str2 { x:5, y:0.99, }; let b = str2 { x:6, y:15.6 };

After simple observation and actual compilation, it can be found that although the Create method can splice two structural instances with different generics to create a new structural instance.
However, obviously, the original two structure instances will be drop ped after creation. The author believes that this is unacceptable in the actual production environment, so he began to improve the method.

2, Improvement process

1. Use immutable references

The improvement code is as follows:

fn create<V, W> (&self, other: &str2<V,W>) -> str2<T,W> { str2 { x:self.x, y:(*other).y, } }

After the above improvements, I thought it could run smoothly, but an error occurred after cargo run. The specific error is as follows:

error[E0507]: cannot move out of `self.x` which is behind a shared reference --> src/main.rs:9:15 | 9 | x:self.x, | ^^^^^^ move occurs because `self.x` has type `T`, which does not implement the `Copy` trait error[E0507]: cannot move out of `other.y` which is behind a shared reference --> src/main.rs:10:15 | 10 | y:(*other).y, | ^^^^^^^^^^ move occurs because `other.y` has type `W`, which does not implement the `Copy` trait

I thought there was a deviation in the application of reference and dereference, but after a series of checks, I found that the problem was not here, but the declaration of generic types

2. Declare generic properties

The improvement code is as follows:

#[derive(Debug)] struct str2<T:PartialOrd + Copy,E:PartialOrd + Copy> { x:T, y:E, } impl<T:PartialOrd + Copy,E:PartialOrd + Copy> str2<T,E> { fn create<V:PartialOrd + Copy,W:PartialOrd + Copy> (&self,other:&str2<V,W>) -> str2<T,W> { str2{ x:self.x, y:(*other).y, } } }

When I first learned about generics, the examples declared the features of all involved generics, which made me wonder whether all generic instances do not have any features by default if the features are not declared in advance when using generics?

So with the idea of trying, I added a feature declaration to the generic declaration in the structure and method. After compiling and running, I found that the error reporting problem was solved. I successfully spliced the two instances into a third new instance and completely retained the two old instances.

fn main() { let a = str2 { x:5, y:0.99, }; let b = str2 { x:6, y:15.6 }; let ab = a.create(&b); println!("{:#?}",ab); println!("{:?}",a); println!("Hello, world!"); }

The operation results are as follows:

Finished dev [unoptimized + debuginfo] target(s) in 0.39s Running `target/debug/re_stru_T` str2 { x: 5, y: 15.6, } str2 { x: 5, y: 0.99 } Hello, world!
summary

To sum up, in the process of solving this problem, the author reviewed the knowledge of citation and de citation, and read some Excellent articles , I am more familiar with the use of generics in structures and methods, and understand the special properties of features in generics. If there is any mistake, please correct it.

29 October 2021, 22:29 | Views: 1317

Add new comment

For adding a comment, please log in
or create account

0 comments