0 day streak
Code library

Code worth reading.

Hand-picked snippets across TypeScript, Python, Rust, Go, and more — each one comes with a line-by-line breakdown so you can read it like the people who wrote it.

3495 snippets · 10 languages
▶ Start learning
Rust · BeginnerSnippet

Control Flow with If Expressions

In Rust, 'if' is an expression, not just a statement. This means it returns a value that can be assigned to a variable, provided all branches return the same type.

snippet.rs
rust
1
2
3
4
5
6
7
8
9
fn main() {
let number = 7;
let condition = if number < 10 {
true
} else {
false
};
println!("Condition is {condition}");
}
Control FlowSyntax
Rust · BeginnerSnippet

Working with Fixed-Size Arrays

Arrays in Rust have a fixed length and must contain elements of the same type. They are useful when you want your data allocated on the stack rather than the heap.

snippet.rs
rust
1
2
3
4
5
6
fn main() {
let a: [i32; 3] = [10, 20, 30];
let first = a[0];
let length = a.len();
println!("First: {first}, Length: {length}");
}
Arrays & ListsData Types
Rust · BeginnerSnippet

Variables and Immutability

In Rust, variables are immutable by default, meaning their value cannot change once bound. To allow a variable to be updated, you must explicitly use the 'mut' keyword.

snippet.rs
rust
1
2
3
4
5
6
7
fn main() {
let x = 5;
// x = 6; // This would cause a compile error
let mut y = 10;
y = 11; // This is allowed
println!("x: {x}, y: {y}");
}
SyntaxData Types
Rust · BeginnerSnippet

Basic Error Handling with Option

Rust uses the Option enum to represent a value that might be missing, instead of using null. 'match' is used to safely handle both the presence (Some) and absence (None) of the value.

snippet.rs
rust
1
2
3
4
5
6
7
8
9
fn main() {
let some_value = Some(5);
let no_value: Option<i32> = None;
match some_value {
Some(val) => println!("Value: {val}"),
None => println!("No value found"),
}
}
Error HandlingData Types
Rust · BeginnerSnippet

Defining Basic Functions

Functions are declared using 'fn'. Parameters must have explicit type annotations, and the return type is specified after an arrow '->'. The last expression in a function is its return value.

snippet.rs
rust
1
2
3
4
5
6
7
8
fn add(a: i32, b: i32) -> i32 {
a + b
}
fn main() {
let sum = add(5, 10);
println!("The sum is: {sum}");
}
FunctionsSyntax
Rust · BeginnerSnippet

Defining Basic Enums

Enums (enumerations) allow you to define a type by enumerating its possible variants. Variants can also hold data.

snippet.rs
rust
1
2
3
4
5
6
7
8
enum WebEvent {
PageLoad,
PageUnload,
KeyPress(char),
}
let load = WebEvent::PageLoad;
let press = WebEvent::KeyPress('x');
SyntaxData Types
Rust · BeginnerSnippet

Using Vectors for Dynamic Lists

Vectors allow you to store a variable number of values next to each other in memory. Unlike arrays, they can grow or shrink.

snippet.rs
rust
1
2
3
4
5
6
let mut v: Vec<i32> = Vec::new();
v.push(5);
v.push(6);
let second = v[1];
println!("The second element is {}", second);
Data TypesSyntax
Rust · BeginnerSnippet

Defining and Instantiating Structs

Structs allow you to create custom data types by grouping related values together. Each field has a name and a type.

snippet.rs
rust
1
2
3
4
5
6
7
8
9
10
11
12
13
struct User {
username: String,
email: String,
active: bool,
}
fn main() {
let user1 = User {
email: String::from("[email protected]"),
username: String::from("someuser123"),
active: true,
};
}
SyntaxData Types