小编The*_*ler的帖子

为什么没有为简单的异步 fn 返回状态实现处理程序特征?

我认为应该为返回 a 的函数Handler实现:asyncStatusCode

use axum::{
    headers,
    http::StatusCode,
    routing::{delete, get, post, put},
    Router,
};

pub fn create_routes() -> Router {
    Router::new().route("/webhook", post(webhook_handler()))
}

#[debug_handler]
async fn webhook_handler() -> StatusCode {
    StatusCode::OK
}
Run Code Online (Sandbox Code Playgroud)
use axum::{
    headers,
    http::StatusCode,
    routing::{delete, get, post, put},
    Router,
};

pub fn create_routes() -> Router {
    Router::new().route("/webhook", post(webhook_handler()))
}

#[debug_handler]
async fn webhook_handler() -> StatusCode {
    StatusCode::OK
}
Run Code Online (Sandbox Code Playgroud)

这看起来与文档中提供的示例几乎相同:

// `StatusCode` gives an empty response with that status code
async fn status() -> StatusCode …
Run Code Online (Sandbox Code Playgroud)

rust rust-axum

3
推荐指数
1
解决办法
1119
查看次数

即使在 if let 语句之后,值仍然包含在 Option 中

我有一个包含两个字段state和children的结构体。Children 是一个包含孩子向量的选项。我正在使用 if let 语法将此选项解构回向量。

fn main() {
    let mut root = Node::new2d(3);
    loop {
        root.calc_scores(100);
        if let Some(mut children) = root.children {
            let child = children.pop();
            root = Node {
                state: child.0,
                children: None,
            }
        }
        root.make_children();
    }
}
struct Node<T> {
    state: T,
    children: Option<Vec<(T, i32)>>,
}
Run Code Online (Sandbox Code Playgroud)

上面的代码不能编译。它抱怨 child 是 类型Option<(Board2d,i32)>。为什么 child 仍然包含在选项枚举中?不会if let Some(mut children) = root.children{}从 Option 枚举中取出向量吗?

rust

0
推荐指数
1
解决办法
134
查看次数

标签 统计

rust ×2

rust-axum ×1