Advertisement

Online Rust Compiler

Write, run and test Rust code online, right in your browser. This free online Rust compiler needs no installation or signup — just type your code and click Run.

Rust is a systems language focused on speed and memory safety without a garbage collector, used for tooling, WebAssembly and high-performance services.

How to use the Rust compiler

  1. Write or paste your Rust code in the editor.
  2. If your program reads input, open the I/O tab and type it into the STDIN box (one value per line).
  3. Click Run — the output and any errors appear in the output panel.

Sample: read input in Rust

use std::io;

fn main() {
    let mut name = String::new();
    io::stdin().read_line(&mut name).unwrap();
    println!("Hello, {}!", name.trim());
}

Paste this into the editor, type your name in the STDIN box and click Run.

More Rust examples to try

Vectors and iterators

Reads numbers from STDIN and uses iterator methods to filter and sum them. Try 3 8 12 5.

use std::io::{self, Read};

fn main() {
    let mut input = String::new();
    io::stdin().read_to_string(&mut input).unwrap();
    let nums: Vec<i32> = input.split_whitespace().map(|x| x.parse().unwrap()).collect();
    let even_sum: i32 = nums.iter().filter(|&&x| x % 2 == 0).sum();
    println!("Numbers: {:?}", nums);
    println!("Sum of even numbers: {}", even_sum);
}

Structs, impl and Option

A struct with methods, and Option to handle a missing value safely.

struct Account {
    owner: String,
    balance: f64,
}

impl Account {
    fn withdraw(&mut self, amount: f64) -> Option<f64> {
        if amount > self.balance {
            None
        } else {
            self.balance -= amount;
            Some(self.balance)
        }
    }
}

fn main() {
    let mut acc = Account { owner: String::from("Asha"), balance: 100.0 };
    match acc.withdraw(30.0) {
        Some(left) => println!("{} has {} left", acc.owner, left),
        None => println!("Insufficient funds"),
    }
}

Common Rust errors and how to fix them

error[E0382]: borrow of moved value
The value was moved to another variable or function. Pass a reference (&value) or call .clone().
error[E0596]: cannot borrow as mutable
Declare the variable with let mut, and pass it as &mut value.
error[E0308]: mismatched types
The value type does not match what is expected, e.g. i32 vs f64. Convert with as f64 or .parse().
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value'
unwrap() found nothing — often missing input. Provide STDIN, or handle the case with match or unwrap_or.

Frequently asked questions

Is this online Rust compiler free?

Yes. It is completely free to use, with no signup required to write and run code.

Do I need to install anything?

No. It all works from your web browser, so there is nothing to download or set up — it works on Windows, macOS, Linux, Chromebooks and phones.

How do I give input to my Rust program?

Before clicking Run, type the input into the STDIN box under the I/O tab, one value per line. Your program reads it as standard input, just like typing in a terminal.

Is this good for learning Rust?

Yes. It is ideal for practising exercises, testing small programs and trying out ideas from tutorials, because you can run code instantly without configuring an environment.

Other online compilers

Editor powered by OneCompiler.