capypad
0 day streak
rust / beginner
Snippet

Explicit Type Annotations

Rust supports explicit type annotations with the colon syntax. While Rust's compiler can infer types, explicit annotations improve code clarity and help catch bugs early. Required when initializing from functions without clear return types.

snippet.rs
rust
1
2
3
4
5
6
7
8
fn main() {
let number: i32 = 42;
let text: &str = "Hello";
let flag: bool = false;
let inferred = 3.14;
println!("{:?}", inferred);
}
Breakdown
1
: i32, : &str, : bool
Type annotations after variable names specify exactly what type each variable must be.