-4

What is equivalent Java's R (stands for Return) in rust?

For example, how can I write something like this in Rust?

<R> R accept(Visitor<R> visitor) {
    return visitor.visitAssignExpr(this);
}

Relating to this post.

bichanna
  • 954
  • 2
  • 21

1 Answers1

7

The equivalent in Rust would be:

fn accept<R>(&self, visitor: Visitor<R>) -> R {
    visitor.visit_assign_expr(self)
}

The R is just an identifier; it doesn't mean anything special and could've just as easily been T or Return. In Rust, the list of generics for a function go between the function name and the parameters. Note: I've made other changes in syntax and convention.

Beyond syntax, Rust's generics may behave or be constrained differently than they are in Java. Consider reading through the Rust Book, it has a chapter on Generic Data Types.

See also:

kmdreko
  • 42,554
  • 6
  • 57
  • 106