I am trying to find a way to destroy a selfmethod argument . According to a comment on GitHub :
At today's meeting, we have another plan to make our own arguments destructible. With the universal function call syntax (UFCS # 11938), there will be no difference between static methods and instance methods - they will both be “related functions”. At this point, any function, the first argument which is a type of self, will be called with the syntax of the method, as well self, &selfand &mut selfare simply sugar for ie The self: &Selfand destructuring on self argument can be performed as usual without using the sugar-sugar.
I wrote the following code, but it does not work as I expected, since all three printing functions can be used as a method.
struct Vector {
x: i32,
y: i32,
z: i32,
}
impl Vector {
fn print1(self: &Self) {
println!("{} {} {}", self.x, self.y, self.z);
}
fn print2(&Vector{x, y, z}: &Self) {
println!("{} {} {}", x, y, z);
}
fn print3(this: &Self) {
println!("{} {} {}", this.x, this.y, this.z);
}
}
fn main() {
let v = Vector{x: 1, y: 2, z: 3};
Vector::print1(&v);
v.print1();
Vector::print2(&v);
v.print2();
Vector::print3(&v);
v.print3();
}
print3()just used to check if a name other than can be used selffor the first argument of the method.
It gives this compilation error:
error: no method named `print2` found for type `Vector` in the current scope
--> 1.rs:27:7
|
27 | v.print2(); // not work
| ^^^^^^
|
= note: found the following associated functions; to be used as methods, functions must have a `self` parameter
note: candidate #1 is defined in an impl for the type `Vector`
--> 1.rs:12:5
|
12 | fn print2(&Vector{x, y, z}: &Self) {
| _____^ starting here...
13 | | println!("{} {} {}", x, y, z);
14 | | }
| |_____^ ...ending here
error: no method named `print3` found for type `Vector` in the current scope
--> 1.rs:29:7
|
29 | v.print3(); // not work
| ^^^^^^
|
= note: found the following associated functions; to be used as methods, functions must have a `self` parameter
note: candidate #1 is defined in an impl for the type `Vector`
--> 1.rs:16:5
|
16 | fn print3(this: &Self) {
| _____^ starting here...
17 | | println!("{} {} {}", this.x, this.y, this.z);
18 | | }
| |_____^ ...ending here
It seems that print2()they are print3()not identified as methods Vector.
- How to destroy a
selfmethod argument ? - According to the comment, the name
selfis just sugar. Does this mean that you can use a name other than the first argument to the method self?