Learn RUST-PLAYGROUND with Real Code Examples
Updated Nov 26, 2025
Code Sample Descriptions
1
Hello World in Rust Playground
fn main() {
println!("Hello, world!");
}
A basic Hello World program written and run in Rust Playground.
2
Rust Variables Example
fn main() {
let x = 42;
let name = "Alice";
println!("x = {}, name = {}", x, name);
}
Declares variables and prints them in Rust Playground.
3
Rust If Statement Example
fn main() {
let num = -3;
if num > 0 {
println!("Positive");
} else {
println!("Non-positive");
}
}
Checks if a number is positive or negative in Rust.
4
Rust Loop Example
fn main() {
for i in 1..=5 {
println!("{}", i);
}
}
Prints numbers from 1 to 5 using a for loop in Rust.
5
Rust Function Example
fn add(a: i32, b: i32) -> i32 {
a + b
}
fn main() {
println!("Sum: {}", add(3, 4));
}
Defines a function to add two numbers and prints the result.
6
Rust Vector Example
fn main() {
let nums = vec![10, 20, 30];
for n in nums {
println!("{}", n);
}
}
Creates a vector and iterates over it in Rust Playground.
7
Rust Mutable Variable Example
fn main() {
let mut counter = 0;
counter += 1;
println!("Counter: {}", counter);
}
Shows mutable variables and updates their values in Rust.
8
Rust Match Statement Example
fn main() {
let x = 2;
match x {
1 => println!("One"),
2 => println!("Two"),
_ => println!("Other"),
}
}
Uses match to handle multiple cases in Rust Playground.
9
Rust Struct Example
struct Person {
name: String,
age: u8,
}
fn main() {
let p = Person { name: String::from("Alice"), age: 30 };
println!("{} is {} years old", p.name, p.age);
}
Defines a struct and prints its fields in Rust Playground.
10
Rust Ownership Example
fn main() {
let s1 = String::from("hello");
let s2 = s1;
// println!("{}", s1); // This would cause an error
println!("{}", s2);
}
Demonstrates basic ownership rules in Rust Playground.