简体   繁体   English

如何使用 Rocket.rs 显示 html 文件

[英]How do I display html files with rocket.rs

I am new to rust and I am trying to build a simple webpage with HTML.我是 rust 新手,我正在尝试用 HTML 构建一个简单的网页。

This is my code:这是我的代码:

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])
}

But it returns the error:但它返回错误:

   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

I know for sure that this module exists because https://api.rocket.rs/v0.4/rocket/response/content/struct.Html.html我确定这个模块存在是因为https://api.rocket.rs/v0.4/rocket/response/content/struct.Html.html

You can either use the full path name to refer to that module, ie.您可以使用完整路径名来引用该模块,即。

#[get("/")]
fn index() -> rocket::response::content::Html<&'static str> {
  rocket::response::content::Html(...)
}

Or add that specific module to the current namespace, with或者将该特定模块添加到当前命名空间,使用

use rocket::response::content;

In your code, you were only adding everything in the rocket module in your namespace with use rocket::* , and the Html struct with use::rocket::response::content::Html , so as is you could also use Html directly:在您的代码中,您只需use rocket::*添加命名空间中的 Rocket 模块中的所有内容,并use::rocket::response::content::Html添加Html结构,因此您也可以使用Html直接地:

fn index() -> Html<&'static str> {
  Html(...)
}

In the (preview) Rocket 0.5.0-rc.2, the struct Html was renamed to RawHtml .在(预览版)Rocket 0.5.0-rc.2 中,struct Html被重命名为RawHtml Replace Html with RawHtml and it'll work.RawHtml替换Html就可以了。

See also the changelog :另请参阅更改日志

  • Content-Type content responder type names are now prefixed with Raw . Content-Type content响应器类型名称现在以Raw为前缀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM