Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is the same as `R` (generics) in Rust?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading