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 π¦.