Skip to main content

Module cargo_runner

Module cargo_runner 

Source
Expand description

Compiles and runs Rust source files that declare external dependencies, or runs existing Cargo projects directly.

§Single-file dependencies

When a .rs file contains embedded Cargo manifest sections in //! doc comments (e.g. [dependencies]), this module creates a temporary Cargo project, writes a proper Cargo.toml, and delegates to cargo run.

//! [dependencies]
//! rand = "0.8"
//! serde = { version = "1.0", features = ["derive"] }

use rand::Rng;
use serde::Serialize;

fn main() {
    let n: i32 = rand::thread_rng().gen_range(1..=100);
    println!("Random number: {n}");
}

§Cargo projects

When pointed at a directory containing a Cargo.toml, virtual-rust will compile and run the project with cargo run directly:

virtual-rust ./my-project

Structs§

EmbeddedManifest
Embedded Cargo manifest extracted from //! doc comments.

Functions§

has_dependencies
Returns true if the source contains an embedded dependency manifest.
is_cargo_project
Returns true if the given path is a Cargo project directory (i.e. it is a directory containing a Cargo.toml).
parse_embedded_manifest
Scans //! doc comments at the top of a Rust file for Cargo manifest sections.
run_cargo_project
Runs an existing Cargo project directory with cargo run.
run_with_cargo
Compiles and runs a Rust source file that has embedded dependencies.