我是 rust 新手,我正在嘗試用 HTML 構建一個簡單的網頁。
這是我的代碼:
use rocket::*;
use rocket::response::content::Html;
#[get("/")]
fn index() -> content::Html<&'static str> {
content::Html(r#"
<title>GCD Calculator</title>
<form action="/gcd" method="post">
<input type="text" name="n" />
<input type="text" name="n" />
<button type="submit">Compute GCD</button>
</form>
"#)
}
#[launch]
fn rocket()->_{
rocket::build().mount("/", routes![index])
}
但它回傳錯誤:
Compiling hello-rocket v0.1.0 (/home/garuda/dev/Rust/Rocket/hello-rocket)
error[E0432]: unresolved import `rocket::response::content::Html`
--> src/main.rs:2:5
|
2 | use rocket::response::content::Html;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no `Html` in `response::content`
error[E0433]: failed to resolve: use of undeclared crate or module `content`
--> src/main.rs:5:15
|
5 | fn index() -> content::Html<&'static str> {
| ^^^^^^^ use of undeclared crate or module `content`
error[E0433]: failed to resolve: use of undeclared crate or module `content`
--> src/main.rs:6:5
|
6 | content::Html(r#"
| ^^^^^^^ use of undeclared crate or module `content`
Some errors have detailed explanations: E0432, E0433.
For more information about an error, try `rustc --explain E0432`.
error: could not compile `hello-rocket` due to 3 previous errors
我確定這個模塊存在是因為https://api.rocket.rs/v0.4/rocket/response/content/struct.Html.html
uj5u.com熱心網友回復:
在(預覽版)Rocket 0.5.0-rc.2 中,該結構Html被重命名為RawHtml. 替換Html為RawHtml,它會作業。
另請參閱更改日志:
- Content-Type
content回應者型別名稱現在以Raw.
uj5u.com熱心網友回復:
您可以使用完整路徑名來參考該模塊,即。
#[get("/")]
fn index() -> rocket::response::content::Html<&'static str> {
rocket::response::content::Html(...)
}
或者將該特定模塊添加到當前命名空間,使用
use rocket::response::content;
在您的代碼中,您只在命名空間中添加了火箭模塊中的所有內容,使用添加use rocket::*結構Html,use::rocket::response::content::Html因此您也可以Html直接使用:
fn index() -> Html<&'static str> {
Html(...)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/497439.html
