Expand description
Compiles and runs Rust source files that declare external dependencies,
runs existing Cargo projects, or assembles loose .rs directories into
temporary Cargo projects.
§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§Loose .rs directories
When pointed at a directory containing .rs files but no Cargo.toml,
virtual-rust will automatically assemble them into a temporary Cargo project.
The file containing fn main() becomes src/main.rs, and all other files
are copied as sibling modules under src/.
virtual-rust ./my-scripts/ # directory with main.rs, utils.rs, etc.Structs§
- Embedded
Manifest - Embedded Cargo manifest extracted from
//!doc comments.
Functions§
- has_
dependencies - Returns
trueif the source contains an embedded dependency manifest. - is_
cargo_ project - Returns
trueif the given path is a Cargo project directory (i.e. it is a directory containing aCargo.toml). - is_
rust_ source_ dir - Returns
trueif the given path is a directory containing.rsfiles but noCargo.toml(i.e. a loose collection of Rust source files). - 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_
rust_ dir - Runs a directory of loose
.rsfiles by generating a temporary Cargo project. - run_
with_ cargo - Compiles and runs a Rust source file that has embedded dependencies.