Skip to content

Data Types πŸ—„οΈβš“

Association Types Table πŸ“–βš“

This is a roughly parallel table for types between Python 🐍 and Rust πŸ¦€.

Python 🐍 Rust πŸ¦€
bool (True or False) bool (true or false)
int i8, i16, i32, i64, i128, isize,
u8, u16, u32, u64, u128, usize
float f32, f64
array.array array
str / bytes (rough) char
str / bytes (rough) str
str ("" or str()) String (String::new())
bytes (b"") str.as_bytes()
tuple (() or tuple()) tuple (();)
ellipsis (...) / NoneType (None) unit (())
list ([] or list()) collections.Vec (Vec::new() or vec![])
dict ({} or dict()) collections.HashMap, collections.BTreeMap
set (set()) collections.HashSet, collections.BTreeSet
collections.deque(deque()) collections.VecDeque (VecDeque::new())
enum.Enum enum
NoneType (None) enum Option<T>
Exception enum Result<T, E>
generator slice (rough)
function fn
lambda (lambda x, y: x + y) closure (|x, y| x + y)
range (range(0, 10)) Range (..10)
decimal.Decimal
fractions.Fraction
pathlib.Path path.Path
class / dataclass struct (rough)
abc.ABC / @abstractmethod trait
datetime.datetime
datetime.time
datetime.timedelta time.Duration (rough)
Box<T> (Box::new())

Association Sizes Table πŸ’Ύβš“

One of the main features of Rust πŸ¦€ is working with memory, so I suggest that you familiarize yourself with this table.

Memory management

Also to clarify, Rust πŸ¦€ stores most of the standard data types on the stack, while Python 🐍 always uses the heap for this.

This is a roughly parallel table for types sizes between Python 🐍 and Rust πŸ¦€ (in bytes).

πŸ’Ύ

8️⃣ bits 🟰 1️⃣ byte πŸ˜‰

Type: 🐍 / πŸ¦€ Value: 🐍 / πŸ¦€ Size: 🐍 πŸ¦€
bool False / true 24 1
bool True / true 28 1
int / i8 -128 βž– 127 28 1
int / i16 -32768 βž– 32767 28 2
int / i32 -2147483648 βž– 2147483647 32 4
int / i64 -9223372036854775808 βž– 9223372036854775807 36 8
int / i128 -170141183460469231731687303715884105728 44 16
int / u8 0 βž– 255 24 1
int / u16 0 βž– 65535 28 2
int / u32 0 βž– 4294967295 32 4
int / u64 0 βž– 18446744073709551615 36 8
int / u128 0 βž– 340282366920938463463374607431768211455 44 16
float / f32 E 2.718281828459045 / 2.7182817 24 4
float / f32 PI 3.141592653589793 / 3.1415927 24 4
float / f32 TAU 6.283185307179586 / 6.2831855 24 4
float / f32 float("-inf") / f32::NEG_INFINITY 24 4
float / f32 float("inf") / f32::INFINITY 24 4
float / f64 E 2.718281828459045 24 8
float / f64 PI 3.141592653589793 24 8
float / f64 TAU 6.283185307179586 24 8
float / f64 float("-inf") / f64::NEG_INFINITY 24 8
float / f64 float("inf") / f64::INFINITY 24 8
array.array / array a = array.array("i", [1]) /
let mut a:[i8, 1] = [1]
84 1
array.array / array b = array.array("q", [1, 2]) /
let mut b:[i128, 2] = [1, 2]
96 32

Boolean βœ…βŽβš“

Python πŸβš“

Docs

In Python bool has different sizes, for True it's equal to 28 bytes, for False is 24 bytes.

1
2
3
4
5
6
7
8
9
import sys

a = True
b = False

print(sys.getsizeof(a))
print(sys.getsizeof(b))
# Output: 28
# Output: 24

Warning

In Python Boolean (bool) type are subclass from Integer (int). So keep it in mind 🀯.

Roughly True is equal to 1 and False is equal to 0.

bool values True / False rougly equavivalent to int values 1 / 0

>>> True == 1
True
>>> False == 0
True

bool type actually instance of int type

>>> issubclass(bool, int)
True

All bool values actually instance of int type

>>> isinstance(bool(), int)
True
>>> isinstance(True, int)
True
>>> isinstance(False, int)
True

Rust πŸ¦€βš“

Docs

In Rust bool type always allocate the same size that equal to 1 byte.

1
2
3
4
5
6
use std::mem::size_of;

fn main() {
    println!("{}", size_of::<bool>());
}
// Output: 1

Examplesβš“

Declarationsβš“

1
2
3
4
5
if __name__ == "__main__":
    x = True
    y = False

    print(f"{x=}, {y=}")
Output
x=True, y=False
1
2
3
4
5
6
fn main() {
    let x = true;
    let y = false;

    println!("x={}, y={}", x, y);
}
Output
x=true, y=false

Conditionsβš“

Boolean conditions works the same in Python 🐍 and Rust πŸ¦€.

if __name__ == "__main__":
    if True:
        print("if True")  # always works code

    if False:
        print("if False")  # unreachable code

    if not True:
        print("if not True")  # unreachable code

    if not False:
        print("if not False")  # always works code

    if True & False:
        print("if True & False")  # unreachable code

    if True & True:
        print("if True & True")  # always works code

    if False & False:
        print("if False & False")  # unreachable code

    if True | False:
        print("if True | False")  # always works code

    if True | True:
        print("if True | True")  # always works code

    if False | False:
        print("if False | False")  # unreachable code
Output
if True
if not False
if True & True
if True | False
if True | True
fn main() {
    if true {
        println!("if true"); // always works code
    }

    if false {
        println!("if false"); // unreachable code
    }

    if !true {
        println!("if !true"); // unreachable code
    }

    if !false {
        println!("if !false"); // always works code
    }

    if true & false {
        println!("if true & false"); // unreachable code
    }

    if true & true {
        println!("if true & true"); // always works code
    }

    if false & false {
        println!("if false & false"); // unreachable code
    }

    if true | false {
        println!("if true | false"); // always works code
    }

    if true | true {
        println!("if true | true"); // always works code
    }

    if false | false {
        println!("if false | false"); // unreachable code
    }
}
Output
if true
if !false
if true & true
if true | false
if true | true