Home > database >  How do I display a html webpage by giving the link with rocket.rs
How do I display a html webpage by giving the link with rocket.rs

Time:06-30

I am able to make a html webpage but with the html code in my .rs file. How do I separate it giving a link to the file to be displayed to rocket.rs?

My code:

use rocket::*;
use rocket::response::content::RawHtml;

#[get("/")]
fn index() -> RawHtml<&'static str> {
    RawHtml(r#"<h1>Hello world</h1>")
}

#[launch]
fn rocket()->_{
    rocket::build().mount("/", routes![index])
}

CodePudding user response:

If you want the HTML document compiled in to your program so that it has no dependencies on external files, you can use the standard include_str! macro:

RawHtml(include_str!("index.html"))

This will look for index.html in the same directory as the .rs file the macro appears in during compilation, and insert the contents as a static string literal.

Alternatively, if you want the program to look for a file on disk at runtime, you can use Rocket's NamedFile responder.

  • Related