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

Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.

To run a standalone Rust file, save a main function in hello.rs, compile it with rustc hello.rs, then execute ./hello on Unix-like systems or .hello.exe on Windows. For a project containing Cargo.toml, use cargo run. If you want one-file scripting with external crates and repeat-run caching, install rust-script and run rust-script hello.rs.

Choose the command that matches your Rust code

Situation Command What it does
One small file with no external crates rustc file.rs, then run the output Invokes the compiler directly and creates a native executable.
Existing Cargo package cargo run Builds the package, resolves dependencies, and runs its selected binary.
Single file that needs crates rust-script file.rs Creates a Cargo project behind the scenes, supports script dependencies, and caches unchanged builds.
Self-executing or associated script files cargo script file.rs Provides the cargo-script workflow, including documented Unix hashbang and Windows association options.

Rust is compiled rather than interpreted like Python. You can make the workflow feel script-like, but every route ultimately compiles Rust before execution.

Run a standalone .rs file with rustc

1. Install and verify Rust

Install the Rust toolchain with the official Rust installation method for your operating system, then open a new terminal. Verify that both the compiler and Cargo are available:

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

If either command is not found, finish the toolchain installation and ensure its binary directory is on your shell’s PATH.

2. Create the file

Save this as hello.rs:

fn main() {
    println!("Hello, Rust!");
}

A runnable Rust executable needs a main function. Unlike a library, a binary has an entry point and can be launched by the operating system.

3. Compile it

rustc hello.rs

On success, rustc writes an executable in the current directory. The default name is hello on Unix-like systems and hello.exe on Windows. Compilation errors stop here and include a source location, an explanation, and often a suggested fix.

4. Execute the binary

On Linux, macOS, and other Unix-like systems:

./hello

On Windows PowerShell:

.hello.exe

On Windows Command Prompt, use hello.exe from the current directory. A successful run prints:

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

Choose an output path explicitly

Use -o when you want a different executable name or location:

rustc hello.rs -o build/hello

Create the destination directory first if it does not exist. This direct compiler workflow is useful for experiments and tiny utilities, but it does not manage a dependency graph or package metadata for you.

Run a normal Rust project with Cargo

Cargo is the standard workflow for most Rust applications. The Rust Project notes that most programmers invoke rustc through Cargo rather than calling the compiler directly.

Create a package

cargo new hello
cd hello

Cargo creates a directory containing Cargo.toml and src/main.rs. The manifest records the package and its dependencies; the source file contains the program entry point.

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

Build and run

cargo run

Run this command from the directory containing Cargo.toml. Cargo resolves dependencies, compiles the package, and launches the resulting binary. Build artifacts normally go under target.

Pass arguments to your program

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

cargo run -- --name Ada

Cargo consumes options before --; the program receives --name and Ada. The same convention works with release builds:

cargo run --release -- --name Ada

--release selects the optimized release profile. It usually takes longer to compile than a debug build, but is appropriate when measuring or distributing performance.

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

Run a particular binary

If a package defines multiple binaries, select one by name:

cargo run --bin tool

Use cargo run --bin tool -- --input data.txt to select the binary and forward arguments to it.

Use rust-script for a one-file experience with crates

rust-script is intended for scripts that should remain in one file but need Cargo-compatible dependencies. Its documentation requires Rust 1.74 or later.

Install it

cargo install rust-script

The command installs the executable into Cargo’s binary directory. If your shell cannot find rust-script afterward, add Cargo’s bin directory to PATH or invoke the installed executable by its full path.

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.

Run a script

rust-script hello.rs

The tool creates and builds a Cargo project under the hood. It normally hides Cargo’s build output; when compilation fails, diagnostic output is shown. Use --cargo-output when you need the underlying Cargo messages during a successful or troubleshooting run.

Understand first-run and cached runs

The first invocation compiles the script and its dependencies. Unchanged scripts can use the cached executable on later invocations, so repeat runs are generally faster. Changing the source or dependency information can trigger a rebuild.

Dependencies in a script

rust-script supports its documented manifest conventions for declaring crates in a single file. Follow the version and syntax documented by the tool you install; dependency resolution still follows Cargo’s rules, including downloading crates and compiling native components when required.

Alternative: cargo-script

cargo-script provides another single-file command:

cargo script hello.rs

Its documented features include dependency information embedded in the script, Unix hashbang execution through run-cargo-script, and Windows file associations for .crs files. These integrations require the platform-specific setup described in its documentation. A normal shell command remains the most portable way to invoke the file.

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

Make a Rust file executable like a script

Portable shell invocation

Keep the file as hello.rs and call the runner explicitly:

rust-script hello.rs

This works without changing file permissions or associating a file type with an operating system.

Unix hashbangs

cargo-script documents Unix hashbang support through run-cargo-script. A hashbang is platform-specific: it is not a Windows solution, and it depends on the helper being installed and discoverable. Test the command from the same environment that will launch the script, especially when it is started by a service or scheduled job with a different PATH.

Windows associations

cargo-script documents associations for .crs files on Windows. Association setup changes how Explorer or the shell opens a file; it does not remove the need for a working Rust installation and a correctly configured runner.

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.

Arguments, input, and output

Arguments

With direct compilation, arguments are passed to the compiled executable:

rustc args.rs
./args first second

With Cargo, put them after --; with rust-script, place them after the script path according to the runner’s command-line syntax. In all cases, your Rust code reads them through std::env::args or a command-line parsing crate.

Standard input and files

A Rust program can read standard input and files using the standard library. The runner does not change those operating-system streams. Use an absolute path or verify the current working directory when a script works in one terminal but fails from an IDE, scheduler, or another directory.

Exit status

A successful main returns an exit status of zero. Return an error from main or call an appropriate exit function when an automation system must detect failure. The shell reports the resulting status after the process exits.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshoot common failures

rustc, cargo, or the runner is not found

  • Confirm installation with rustc --version and cargo --version.
  • Open a new terminal after installation so environment changes are loaded.
  • Check that Cargo’s bin directory is on PATH after installing rust-script or cargo-script.

Permission denied on Unix

Run the generated file with ./hello, not just hello. If you are executing a hand-created script rather than a compiled binary, verify its executable permission and hashbang setup. Calling the runner explicitly avoids many permission and association issues.

“No such file or directory”

Check the current directory with pwd (Unix-like systems) or Get-Location (PowerShell). Compile from the directory containing the source, or provide the correct relative or absolute path.

Missing main function

rustc file.rs builds a binary by default. Add fn main() { ... }, or compile a library with the appropriate crate type instead of trying to execute it.

Unresolved external crate

Direct rustc does not replace Cargo’s dependency resolution. Move the code into a Cargo package, use cargo run, or use rust-script with its documented dependency declaration.

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

Compilation is unexpectedly slow

Dependency-heavy scripts pay a first-build cost. Keep the file unchanged between runs to benefit from rust-script’s cache, or use a Cargo project where incremental development builds and explicit profiles are easier to manage.

Cargo runs the wrong target

Run the command from the intended package directory and use --bin when several binaries exist. For a workspace, select the package explicitly with Cargo’s package options.

Reliability and cost considerations

  • Reproducibility: Cargo manifests and lockfiles make dependencies explicit; a bare file compiled with rustc leaves dependency management to you.
  • Portability: Shell commands work across platforms with different executable names; hashbangs and file associations need platform-specific configuration.
  • Build time: Direct rustc is simple for code using only the standard library. Cargo and script runners add dependency and project setup, then can avoid repeated work through build caches.
  • Distribution: A compiled executable can run without the Rust compiler on the target machine, subject to its platform and runtime/native-library requirements.

Or skip the browser setup

If your Rust automation ultimately needs website screenshots, ScreenshotNeo provides a one-request API instead of requiring you to install and manage a headless browser. The API accepts a URL and returns a PNG, JPEG, WebP, or PDF. Before capture it accepts cookie or consent banners and removes more than 60 known consent platforms, newsletter popups, and chat widgets; each cleanup step can be disabled.

Only clean shots are billed. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits cost nothing, and the response identifies the result with X-Page-Verdict and X-Billed headers. ScreenshotNeo also offers an MCP server with take_screenshot, get_page_info, and capture_pdf tools for Claude, Cursor, and other MCP clients.

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

One-call example

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

See the ScreenshotNeo documentation for authentication, output formats, and the full option set. You can also use Python:

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)

Or 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}`);

The free plan includes 1,000 screenshots per month with no card. Paid plans start at $5 for 3,000 shots. Create a free ScreenshotNeo account.

Frequently Asked Questions

Can I run a Rust file without Cargo?

Yes. A standalone file that uses no external crates can be compiled with rustc and executed as the resulting binary.

Which command should I use for daily development?

Use cargo run inside a Cargo package; it is the standard workflow for dependency management, builds, and argument forwarding.

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

Is rust-script an interpreter?

No. It provides a script-style interface but compiles the Rust file, caching unchanged builds for later runs.

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.