Calling an impl method from another impl method

user266003

It seems I can't call the method of the same struct in Rust or maybe I don't understand something:

struct St1 {
  aa: String
}

impl St1 {
  pub fn method1() -> String {
    //....
    method2() //error: unresolved name method2
  }

  pub fn method2() -> String {
    //....
  }
}

Is this how it's supposed to be?

Shepmaster

You need to fully qualify the method you are calling.

struct St1 {
    aa: String
}

impl St1 {
    pub fn method1() -> String {
        St1::method2()
    }

    pub fn method2() -> String {
        unimplemented!()
    }
}

On the off chance that you mean for these methods to be instance methods, then you still have to fully qualify them, but using self instead:

struct St1 {
    aa: String
}

impl St1 {
    pub fn method1(&self) -> String {
        self.method2()
    }

    pub fn method2(&self) -> String {
        unimplemented!()
    }
}

Note that Rust style is 4-space indents.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Calling a method from another method

From Javascript

Calling a method from another method in the same class

From Java

Calling method of Another class from run() method

From Java

java calling a method from another class

From Java

calling another method from the main method in java

From Java

Calling service method from another service class

From Java

NullPointerException when calling method from another class

From Java

Calling a method in one fragment from another

From Dev

Calling a method through from another controller JavaFx

From Dev

How can I return a &str from Strings in an enum in a method impl?

From Dev

calling render method from another class

From Dev

Calling prototype method from another object

From Dev

Calling a method from another jar file

From Dev

Calling a class from another class with main method

From Dev

Calling method from another class unsuccessful

From Dev

Calling a method from inside of another class

From Dev

Laravel: calling controller method from another location

From Dev

Calling a method of the MainActivity from another class?

From Dev

Calling a method from another controller in rails

From Dev

Issues with calling method from another form

From Dev

calling different Action Method from another Controller

From Dev

Exiting from a Method by calling another Method

From Dev

Calling one method from another method in Python

From Dev

JGroups RpcDispatcher calling method from another class

From Dev

Calling Test Method from another Test Method

From Dev

Sinon stub method calling from another file

From Dev

calling an object method from another method not working

From Dev

`libc::signal` for `impl` method

From Dev

Calling Javascript prototype method from another method

Related Related

  1. 1

    Calling a method from another method

  2. 2

    Calling a method from another method in the same class

  3. 3

    Calling method of Another class from run() method

  4. 4

    java calling a method from another class

  5. 5

    calling another method from the main method in java

  6. 6

    Calling service method from another service class

  7. 7

    NullPointerException when calling method from another class

  8. 8

    Calling a method in one fragment from another

  9. 9

    Calling a method through from another controller JavaFx

  10. 10

    How can I return a &str from Strings in an enum in a method impl?

  11. 11

    calling render method from another class

  12. 12

    Calling prototype method from another object

  13. 13

    Calling a method from another jar file

  14. 14

    Calling a class from another class with main method

  15. 15

    Calling method from another class unsuccessful

  16. 16

    Calling a method from inside of another class

  17. 17

    Laravel: calling controller method from another location

  18. 18

    Calling a method of the MainActivity from another class?

  19. 19

    Calling a method from another controller in rails

  20. 20

    Issues with calling method from another form

  21. 21

    calling different Action Method from another Controller

  22. 22

    Exiting from a Method by calling another Method

  23. 23

    Calling one method from another method in Python

  24. 24

    JGroups RpcDispatcher calling method from another class

  25. 25

    Calling Test Method from another Test Method

  26. 26

    Sinon stub method calling from another file

  27. 27

    calling an object method from another method not working

  28. 28

    `libc::signal` for `impl` method

  29. 29

    Calling Javascript prototype method from another method

HotTag

Archive