Introduzione a Rocket in Rust

Mattepuffo's logo
Introduzione a Rocket in Rust

Introduzione a Rocket in Rust

Rocket è un web framework per Rust.

Nel complesso mi sembra completo, anche se la documentazione non mi sembra proprio facile da leggere.

In questo articolo vediamo un esempio introduttivo.

Prima di tutto aggiungiamo la dipendenza al Cargo.toml:

[dependencies]
rocket = "0.5.0-rc.3"

Qui sotto un esempio con due rotte:

#[macro_use]
extern crate rocket;

#[get("/")]
fn index() -> &'static str {
    "ROOT!"
}

#[get("/nome/<nome>")]
fn tuo_nome(nome: &str) -> String {
    format!("Ciao, {}!", nome)
}

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

Enjoy!


Condividi

Commentami!