Rust Data Types for Python Developers
Scalar Types
Integers
Python:
x = 5
y = -3
Rust:
fn main() {
let x: i32 = 5;
let y: u32 = 10;
println!("x = {}, y = {}", x, y);
}
Floating-Point Numbers
Python:
pi = 3.14
Rust:
fn main() {
let pi: f64 = 3.14; // f64 is a 64-bit floating-point number
println!("pi = {}", pi);
}
Booleans
Python:
is_active = True
is_deleted = False
Rust:
fn main() {
let is_active: bool = true;
let is_deleted: bool = false;
println!("is_active = {}, is_deleted = {}", is_active, is_deleted);
}
Characters
Python:
letter = 'a'
Rust:
fn main() {
let letter: char = 'a';
println!("letter = {}", letter);
}
Compound Types
Tuples
Python:
point = (3, 4.5)
Rust:
fn main() {
let point: (i32, f64) = (3, 4.5);
println!("point = ({}, {})", point.0, point.1);
}
Arrays
Python:
numbers = [1, 2, 3, 4, 5]
Rust:
fn main() {
let numbers: [i32; 5] = [1, 2, 3, 4, 5];
println!("numbers = {:?}", numbers);
}
Strings
Python:
greeting = "Hello, world!"
Rust:
fn main() {
let greeting: &str = "Hello, world!";
let mut dynamic_greeting: String = String::from("Hello, world!");
dynamic_greeting.push_str(" Welcome!");
println!("{}", dynamic_greeting);
}
read next tutorial use enums
Top comments (0)