【问题标题】:What is the same as `R` (generics) in Rust?Rust 中的“R”(泛型)有什么区别?
【发布时间】:2022-02-03 04:19:44
【问题描述】:

Rust 中等效的 Java 的R(代表Return)是什么?

例如,我怎样才能在 Rust 中写出这样的东西?

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

this post有关。

【问题讨论】:

  • 不要标记垃圾邮件。 Rust 也有 generics。你在问什么?

标签: java generics rust


【解决方案1】:

Rust 中的等价物是:

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

R 只是一个标识符;这并不意味着什么特别的,可以很容易地成为TReturn。在 Rust 中,函数的泛型列表位于函数名称和参数之间。注意:我对语法和约定做了其他更改。

除了语法之外,Rust 的泛型在行为或约束上可能与在 Java 中不同。考虑阅读Rust Book,它有一章关于Generic Data Types

另见:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-29
    • 1970-01-01
    • 1970-01-01
    • 2010-12-01
    • 1970-01-01
    • 2021-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多