[英]Rust Rocket mismatched types
我刚刚从火箭文档中复制了示例并且有错误。
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use]
extern crate rocket;
#[get("/person/<name>?<age>")]
fn person(name: String, age: u8) -> String {
let mike = uri!(person: "Mike Smith", 28);
assert_eq!(mike.to_string(), "/person/Mike%20Smith?age=28");
}
fn main() {
rocket::ignite().mount("/", routes![person]).launch();
}
错误信息:
main.rs|7 col 37 error| mismatched types expected struct `std::string::String`, found () note: expected type `std::string::String` found type `()` [E0308]
main.rs|7 col 4 info| mismatched types implicitly returns `()` as its body has no tail or `return` expression note: expected type `std::string::String` found type `()` [E0308]
为什么从示例中复制会给我错误?
这是我的第一篇文章:欢呼:
它在抱怨是因为您指定了返回类型,而不是返回它。 当我运行这段代码时,编译器给出了一个更有帮助的答案,你下次应该在你的问题中包含一些内容:
$> cargo run
. . .
error[E0308]: mismatched types
--> src/main.rs:7:37
|
7 | fn person(name: String, age: u8) -> String {
| ------ ^^^^^^ expected struct `std::string::String`, found `()`
| |
| implicitly returns `()` as its body has no tail or `return` expression
error: aborting due to previous error
要么要返回String
类型,要么将返回类型转换为函数:
#[get("/person/<name>?<age>")]
fn person(name: String, age: u8) -> () {
let mike = uri!(person: "Mike Smith", 28);
assert_eq!(mike.to_string(), "/person/Mike%20Smith?age=28");
// or leave the return type as string, but return a string
//return name
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.