我对拉斯特很陌生.
以下功能:
async fn create(body: String) -> impl IntoResponse {
let j = match serde_json::from_str::<CreationJSON>(&body) {
Ok(j) => j,
Err (_) => (
StatusCode::UNPROCESSABLE_ENTITY,
"body is invalid".to_string(),
),
};
println!("{:?}", j.record_stringified);
(
StatusCode::CREATED,
"created".to_string(),
)
}
给出错误:
error[E0308]: `match` arms have incompatible types
--> src/main.rs:128:20
|
126 | let j = match serde_json::from_str::<CreationJSON>(&body) {
| ------------------------------------------------- `match` arms have incompatible types
127 | Ok(j) => j,
| - this is found to be of type `CreationJSON`
128 | Err (_) => (
| ____________________^
129 | | StatusCode::UNPROCESSABLE_ENTITY,
130 | | "body is invalid".to_string(),
131 | | ),
| |_________^ expected `CreationJSON`, found `(StatusCode, String)`
|
= note: expected struct `CreationJSON`
found tuple `(axum::http::StatusCode, std::string::String)`
但是,如果我在Err
号臂上加上一个"回车",那么它就可以正常工作了:
async fn create(body: String) -> impl IntoResponse {
let j = match serde_json::from_str::<CreationJSON>(&body) {
Ok(j) => j,
Err (_) => return (
StatusCode::UNPROCESSABLE_ENTITY,
"body is invalid".to_string(),
),
};
println!("{:?}", j.record_stringified);
(
StatusCode::CREATED,
"created".to_string(),
)
}
为什么我需要在Err
臂中加上return
?我以为没有分号的函数应该在Rust中自动返回?