如何在Rust中引用函数?

huangapple go评论69阅读模式
英文:

How do I reference functions in Rust?

问题

尝试将函数分配给一个变量,在JavaScript中,你可以这样做:

someFunc = someFuncAnotherTile

这样你可以保留参数。

在Rust中,你可以在一个impl块中这样做,例如:

#[OpenApi]
impl TestRoutesApi {
    // 旧烦人的方式
    // #[oai(path = "/test-hello", method = "get")]
    // pub async fn test_hello(&self) -> PlainText<String> {
    //     PlainText(format!("hello"))
    // }

    // 传递函数,但它不起作用。
    #[oai(path = "/test-hello", method = "get")]
    pub async fn test_hello -> test_hello;
}

请注意,代码中的"应该被替换为正常的双引号(")以使其有效。

英文:

Trying to forward the function to a variable, in Javascript you might have

someFunc = someFuncAnotherTile

This way you can preserve the parameters.

In rust, how can I do this inside an implements for example

#[OpenApi]
impl TestRoutesApi {
    // Old annoying way
    // #[oai(path = "/test-hello", method = "get")]
    // pub async fn test_hello(&self) -> PlainText<String> {
    //     PlainText(format!("hello"))
    // }

    // Passing function, but it doesn't work.
    #[oai(path = "/test-hello", method = "get")]
    pub async fn test_hello -> test_hello;

答案1

得分: 2

这不可能,它可能在此RFC之后或选择了后继者后变得可用。

还有delegate,它可以帮助减少繁琐的代码,当一个结构体只是将方法调用传递给其字段之一时。

英文:

It's not possible, it might become available after this RFC or a successor is picked up again.

There is delegate which helps with the boilerplate when a struct simply passes the method call to one of it's fields.

huangapple
  • 本文由 发表于 2023年3月15日 21:26:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75745355.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定