Crates.io | cargo-single |
lib.rs | cargo-single |
version | 1.0.0 |
source | src |
created_at | 2020-07-14 12:59:04.231167 |
updated_at | 2020-07-14 12:59:04.231167 |
description | Extends Cargo to simplify working with single-file programs with dependencies. |
homepage | https://github.com/inejge/cargo-single |
repository | https://github.com/inejge/cargo-single |
max_upload_size | |
id | 265042 |
size | 25,085 |
To write a relatively simple Rust program, which can fit in a single source
file but does need a couple of external dependencies, one must use Cargo to
create a project for the program. Cargo's defaults and tools like cargo-edit
help, but it's still some amount of ceremony and increased friction. This tool
lets one list the dependencies in the comments at the top of the source file,
and use that list and the file name to automatically generate the project
directory, which is then transparently used to check, build or run the program.
You must have Rust and Cargo installed and working. Run:
cargo install cargo-single
See the Cargo documentation to learn how
cargo install
works and how to set up your system to find the installed
binaries.
Create the source file for your program; as an example, save the following
as random.rs
.
// rand = "0.7"
use rand::Rng;
fn main() {
println!("{}", rand::thread_rng().gen_range(1, 11));
}
List the dependencies as comments at the top of the file. Each dependency line
must start with the string "// "
from the leftmost column, and continue in the
format used in the [dependencies]
section of Cargo.toml
. End the list of
dependencies with a blank line.
To build and execute the program, run:
cargo single run random.rs
The tool is invoked through Cargo, with the syntax:
cargo single <command> [<option> ...] {<source-file>|<source-dir>} [<arguments>]
Command is one of: build, check, refresh, or run. Refresh will
re-read the source file and update the dependencies in Cargo.toml
, while the remaining
three are regular Cargo sub-commands which will be passed to Cargo.
Options are a subset of options accepted by Cargo subcommands. The ones recognized by
cargo-single
are:
+toolchain: Name of a toolchain which will be used for building.
--release: Build in release mode.
--target target: Use the specified target for building.
--no-quiet: Don't pass --quiet
to Cargo.
Either the name of the source file, with the .rs
extension, or of the project directory,
which has the same name without the extension, must be given to identify the program.
The remaining arguments, if any, will be passed to the program if it's executed.
Licensed under either of:
at your option.