Не получается запустить сайт на Rust

Рейтинг: 1Ответов: 1Опубликовано: 16.02.2023

структура проекта Структура проекта

main.rs

#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate rocket;
use rocket_contrib::templates::Template;
use std::collections::HashMap;

#[get("/")]
fn index() -> Template {
    let mut context = HashMap::new();
    context.insert("message", "Hello, World!");
    Template::render("index", &context)
}

fn rocket() -> rocket::Rocket {
    rocket::ignite()
        .mount("/", routes![index])
        .attach(Template::fairing())
}

fn main() {
    rocket().launch();
}

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Rocket Rust Example</title>
  </head>
  <body>
    <h1>{{ message }}</h1>
  </body>
</html>

Cargo.toml

[package]
name = "my-rocket-project"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rocket = "0.4.11"
rocket_contrib = { version = "0.4.11", features = ["tera_templates"] }

Вывод:

GET / text/html:
    => Matched: GET / (index)
    => Error: Template 'index' does not exist.
    => Known templates: 
    => Searched in '"D:\\Development\\Rust\\my-rocket-project\\templates"'.
    => Outcome: Failure
    => Warning: Responding with 500 Internal Server Error catcher.
    => Response succeeded.

Ответы

▲ 0

Переименуйте файл index.html в index.tera и перепроверьте дефолтную папку с шаблонами.