Macro rocket::fs::relative

source ·
pub macro relative($path:expr) {
Expand description

Generates a crate-relative version of a path.

This macro is primarily intended for use with FileServer to serve files from a path relative to the crate root.

The macro accepts one parameter, $path, an absolute or (preferably) relative path. It returns a path as an &'static str prefixed with the path to the crate root. Use Path::new(relative!($path)) to retrieve an &'static Path.


Serve files from the crate-relative static/ directory:

use rocket::fs::{FileServer, relative};

fn rocket() -> _ {
    rocket::build().mount("/", FileServer::from(relative!("static")))

Path equivalences:

use std::path::Path;

use rocket::fs::relative;

let manual = Path::new(env!("CARGO_MANIFEST_DIR")).join("static");
let automatic_1 = Path::new(relative!("static"));
let automatic_2 = Path::new(relative!("/static"));
assert_eq!(manual, automatic_1);
assert_eq!(automatic_1, automatic_2);