What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For one standalone Rust file, compile it with rustc, then run the generated executable. Use cargo run when the code belongs to a Cargo package, or rust-script when you want a script-like single file that can declare dependencies and cache its build.

The right command depends on whether you have a single .rs file, a Cargo.toml project, external crates, or a need for self-executing script files.

Choose the command that matches your Rust code

Situation Recommended command What it does
One small file with no external crates rustc file.rs, then run the binary Invokes the compiler directly and produces a native executable.
A Cargo package with Cargo.toml cargo run Resolves the package target and dependencies, builds it, and runs it.
A single-file script that needs crates or repeated execution rust-script file.rs Creates a Cargo build behind the scenes, supports script dependency declarations, and caches unchanged builds.
Self-executing or associated script files cargo script file.rs Provides documented hashbang support on Unix and file-association options on Windows.

Rust can be run from a shell on every supported desktop platform. The executable name and path differ between Unix-like systems and Windows, while hashbangs and file associations require platform-specific setup.

Run a standalone .rs file with rustc

A standalone file needs a main function. Save this as hello.rs:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
fn main() {
    println!("Hello, world!");
}

Open a terminal in the directory containing the file and compile it:

rustc hello.rs

On Unix-like systems, run the resulting executable with:

./hello

On Windows, run:

.hello.exe

The compiler writes the executable beside the source file unless you provide a different output path. For example:

rustc hello.rs -o greeting
./greeting

On Windows, the output is normally an .exe file. Direct rustc compilation is intentionally minimal: it does not create a project, resolve a dependency graph from Cargo.toml, or provide Cargo’s package workflow.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Pass arguments to a directly compiled program

Arguments belong after the executable name, not after rustc. A program can read them with std::env::args:

fn main() {
    let args: Vec<String> = std::env::args().collect();
    println!("{args:?}");
}
rustc args.rs
./args first second

Compile again whenever the source changes. For a larger program or one with dependencies, switch to Cargo rather than maintaining compiler commands manually.

Run a normal Rust project with Cargo

Cargo is the standard workflow for a Rust package. Create a binary package and enter its directory:

cargo new hello
cd hello

Then build and run the package:

cargo run

Cargo reads Cargo.toml, selects the package in the current directory by default, resolves dependencies, compiles the target, and launches it. The default binary source is src/main.rs.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Forward command-line arguments

Put a double dash between Cargo’s options and your program’s arguments:

cargo run -- --name Ada

Everything after -- is passed to the binary. Without the separator, Cargo attempts to interpret those words as its own options.

Run a particular binary or mode

Packages can contain multiple binaries. Cargo’s normal selection rules apply; when you need a specific target, use Cargo’s target options, then place application arguments after the separator. For development diagnostics, Cargo also provides its usual build profiles, including the unoptimized development profile and the optimized release profile:

cargo run
cargo run --release

Use cargo run while iterating and cargo run --release when you need the release profile’s optimized build behavior.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use rust-script for a dependency-aware single file

If you want to keep one visible .rs file but still use crates, install the current tool:

cargo install rust-script

Its documentation requires Rust 1.74 or later. Run a script with:

rust-script hello.rs

rust-script creates and builds a Cargo project under the hood. It normally hides Cargo output; use its documented --cargo-output option when you need to see that build output. The first invocation compiles the script. Later runs of an unchanged script can reuse cached artifacts, which avoids repeating the full initial build.

When a script needs a crate

Use rust-script’s documented manifest conventions in the script to declare dependencies. The runner then supplies those declarations to the generated Cargo project. This is useful for utilities that would be awkward to turn into a complete repository but need functionality outside the standard library.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose rust-script when the file should remain script-shaped, dependencies are important, and the same script will be run repeatedly. Choose Cargo directly when the project has multiple modules, tests, binaries, or a repository lifecycle.

Alternative: cargo script

cargo script hello.rs is another single-file runner:

cargo script hello.rs

Its documented features include reading dependency information embedded in the script, Unix hashbang execution through run-cargo-script, and Windows file associations for .crs files. These integrations can make a script feel closer to an executable command, but they require the platform-specific setup described by that tool.

Make the choice by workflow

  • Use rustc for a small, self-contained file and the clearest possible compiler workflow.
  • Use cargo run for a package with Cargo.toml, dependencies, tests, or more than one source file.
  • Use rust-script for one-file programs that need crates and benefit from cached repeat builds.
  • Use cargo script when hashbang execution or documented file associations are central to your workflow.

Common failures and fixes

“command not found: rustc” or “cargo is not recognized”

The Rust toolchain is not available on your shell’s PATH, or it is not installed. Install a Rust toolchain, open a new terminal so the environment is refreshed, and verify with:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
rustc --version
cargo --version

“couldn’t read” or “No such file or directory”

Your terminal is in a different directory from the source file. List the directory, change into the folder containing the file, or provide a path:

rustc path/to/hello.rs

Compilation succeeds but the executable will not start on Unix

Run the local file with ./; typing only hello asks the shell to search its command path rather than the current directory:

./hello

Windows cannot find the output

Use the generated .exe name and Windows path syntax:

.hello.exe

Dependencies cannot be found

A direct rustc command does not resolve a package’s dependencies from Cargo.toml. Run the code from its Cargo package with cargo run, or use a script runner with the dependency declaration format it documents.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Arguments are rejected by Cargo

Insert -- before application arguments:

cargo run -- --input data.json

The first script run is slow

rust-script must generate and compile its Cargo project on the first invocation. Subsequent runs of an unchanged script can use its cache. If compilation fails, rerun with --cargo-output to expose the underlying Cargo diagnostics.

A hashbang works on one machine but not another

Hashbang execution is a Unix-oriented integration and is not automatically portable to Windows. Use the shell command explicitly, or follow the runner’s documented Windows association procedure.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Performance, repeatability, and maintenance

rustc has the least orchestration overhead for a single file, but you must manage every compiler invocation yourself. Cargo adds project and dependency management, making builds repeatable as the codebase grows. Script runners add a convenient single-file interface; their first run includes generated-project and dependency compilation, while cached unchanged scripts start faster afterward.

For reliable automation, run commands from a known working directory, keep the Rust toolchain consistent across machines, and make the distinction between build arguments and program arguments explicit with --. Use release mode only when you need Cargo’s optimized release profile; it is not a substitute for fixing compilation errors.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Or skip the browser setup

If you need a clean screenshot of a documentation page, demo, or web output while working on your Rust project, ScreenshotNeo provides a single HTTP request instead of a local browser automation setup. It accepts cookie and consent banners before capture and removes more than 60 known consent platforms, newsletter popups, and chat widgets. Bot checks, CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and response headers identify the page verdict and billing status.

Use the API documented at https://screenshotneo.com/docs/:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

The same request in Python is:

import requests
r = requests.get("https://api.screenshotneo.com/v1/shot", params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"}, timeout=90)
open("shot.webp", "wb").write(r.content)

And in Node.js:

const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);

ScreenshotNeo also offers an MCP server with take_screenshot, get_page_info, and capture_pdf tools for Claude, Cursor, and other MCP clients. The Free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 shots. Sign up for the free plan.

Frequently Asked Questions

Does running a Rust file create a script interpreter process?

No. rustc compiles the source into a native executable, which you run separately. Script runners provide convenience around compilation rather than interpreting Rust line by line.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Should I commit the executable produced by rustc?

Usually no. Keep the Rust source and project files under version control and rebuild the executable for the target operating system.

Can one Cargo package contain several runnable programs?

Yes. Cargo supports multiple binary targets; select the intended target with Cargo’s binary options and put that program’s arguments after --.

What Rust version does rust-script require?

Its documentation states that Rust 1.74 or later is required.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.