Online Rust Compiler
main.rs
Ready
About Rust
Rust is a fast systems language that prevents whole classes of memory bugs at compile time, through its ownership rules.
How to take input in Rust
Read all of stdin into a String, then split it into lines and parse the values you need.
let mut input = String::new(); io::stdin().read_to_string(&mut input).unwrap(); let n: i32 = input.trim().parse().unwrap();
Rust programs to try
Copy one into the editor above, put any input in the Input tab and press Run.
Ownership and borrowing
fn length(s: &String) -> usize {
s.len()
}
fn main() {
let name = String::from("techsnap");
println!("{} has {} letters", name, length(&name));
}Iterators
fn main() {
let nums = vec![1, 2, 3, 4, 5, 6];
let squares: Vec<i32> = nums.iter().filter(|n| *n % 2 == 0).map(|n| n * n).collect();
println!("{:?}", squares);
}Match
fn main() {
let marks = 82;
let grade = match marks {
90..=100 => "A",
75..=89 => "B",
_ => "C",
};
println!("{}", grade);
}Common Rust errors and how to fix them
- error[E0382]: borrow of moved value
- The value was moved into another variable or function. Pass a reference (&x) or clone it.
- error[E0384]: cannot assign twice to immutable variable
- Variables are immutable by default. Declare it with let mut.
- thread 'main' panicked at ... called `Option::unwrap()` on a `None` value
- unwrap() found nothing, often a missing input line. Check the Input tab.
Frequently asked questions
Is this online Rust compiler free?
Yes. Write and run Rust code in your browser for free, with nothing to install and no setup.
How do I give input to my Rust program?
Type it in the Input tab before pressing Run and read it with io::stdin().read_to_string, then split it into lines.
Can I use crates from crates.io?
This compiler runs single-file programs with the standard library.
Does it work on my phone?
Yes. The editor, input and output work in a mobile browser, so you can practise Rust without a laptop.