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 🐍⚓
In Python bool has different sizes, for True it's equal to 28 bytes, for False is 24 bytes.
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
Rust 🦀⚓
In Rust bool type always allocate the same size that equal to 1 byte.
Examples⚓
Declarations⚓
Conditions⚓
Boolean conditions works the same in Python 🐍 and Rust 🦀.