Is it possible to destroy the argument of the `self` method?

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);
    }

    // destructure self argument
    fn print2(&Vector{x, y, z}: &Self) {
        println!("{} {} {}", x, y, z);
    }

    // use another name for the first argument
    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); // work
    v.print1();         // work
    Vector::print2(&v); // work
    v.print2();         // not work
    Vector::print3(&v); // work
    v.print3();         // not work
}

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?
+4
source share
1 answer

, , , , fn foo(bar: &Self) fn foo(self: &Self), - .

github

self let :

let &Vector { x, y, z } = self;
+8

Source: https://habr.com/ru/post/1675584/


All Articles