Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Cargo Script templates

Experimental single-file packages in cargo

Plain

#!/usr/bin/env -S cargo +nightly -q -Zscript
---cargo
package.edition = "2024"
---

fn main() {
    println!("hello rust!");
}

CLI

#!/usr/bin/env -S cargo +nightly -q -Zscript
---cargo
package.edition = "2024"
[dependencies]
xflags = "0.3.2"
---

use std::path::PathBuf;

fn main() {
    let flags = xflags::parse_or_exit! {
        /// Remove directories and their contents recursively.
        optional -r,--recursive
        /// File or directory to remove
        required path: PathBuf
    };

    println!(
        "removing {}{}",
        flags.path.display(),
        if flags.recursive { "recursively" } else { "" },
    )
}

Simple CGI response

#!/usr/bin/env -S cargo +nightly -q -Zscript
---cargo
package.edition = "2024"
[dependencies]
deutsche-bahn-delay-reasons = "1.0"
---

use deutsche_bahn_delay_reasons::Grund;

fn main() {
    let grund = Grund::default();
    println!("Status: 200");
    println!("content-type: text/html; charset=utf-8");
    println!();
    println!("<h1>{grund}</h1>");
}
Last change: , commit: 82060bc