DEV Community

ahmed elboshi
ahmed elboshi

Posted on

Rust for Python developers : Rust Data Types for Python Developers

Rust Data Types for Python Developers

Scalar Types

Integers

Python:

x = 5
y = -3
Enter fullscreen mode Exit fullscreen mode

Rust:

fn main() {
    let x: i32 = 5;
    let y: u32 = 10;
    println!("x = {}, y = {}", x, y);
}
Enter fullscreen mode Exit fullscreen mode

Floating-Point Numbers

Python:

pi = 3.14
Enter fullscreen mode Exit fullscreen mode

Rust:

fn main() {
    let pi: f64 = 3.14;  // f64 is a 64-bit floating-point number
    println!("pi = {}", pi);
}
Enter fullscreen mode Exit fullscreen mode

Booleans

Python:

is_active = True
is_deleted = False
Enter fullscreen mode Exit fullscreen mode

Rust:

fn main() {
    let is_active: bool = true;
    let is_deleted: bool = false;
    println!("is_active = {}, is_deleted = {}", is_active, is_deleted);
}
Enter fullscreen mode Exit fullscreen mode

Characters

Python:

letter = 'a'
Enter fullscreen mode Exit fullscreen mode

Rust:

fn main() {
    let letter: char = 'a';
    println!("letter = {}", letter);
}
Enter fullscreen mode Exit fullscreen mode

Compound Types

Tuples

Python:

point = (3, 4.5)
Enter fullscreen mode Exit fullscreen mode

Rust:

fn main() {
    let point: (i32, f64) = (3, 4.5);
    println!("point = ({}, {})", point.0, point.1);
}
Enter fullscreen mode Exit fullscreen mode

Arrays

Python:

numbers = [1, 2, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

Rust:

fn main() {
    let numbers: [i32; 5] = [1, 2, 3, 4, 5];
    println!("numbers = {:?}", numbers);
}
Enter fullscreen mode Exit fullscreen mode

Strings

Python:

greeting = "Hello, world!"
Enter fullscreen mode Exit fullscreen mode

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);
}
Enter fullscreen mode Exit fullscreen mode

read next tutorial use enums

Top comments (0)