Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
This article was originally published on Medium
|Meaning||Python||Rust||Rust Overloading Trait|
|Add two numbers||+||+||std::ops::Add|
|Subtract right operand from the left||-||-||std::ops::Sub|
|Multiply two numbers||*||*||std::ops::Mul|
|Divide left operand by the right one||/||/||std::ops::Div|
Python and Rust share the same symbols as you see in the above table. Rust calls
% as Remainder instead of the Modulus.
We will cover “Rust Overloading Trait” later in the Operator Overloading.
** symbol for exponents:
powf depends on the type:
f32 have a group of built-in methods. All the integer types
usize have the
pub fn pow(self, exp: u32) -> i32
The above definition tells you that using the
pow method raises self to the power of
exp (which is
u32) and returns
i32 (a signed integer).
The floating-point types,
powi raises a number to an integer power and
powf raises a number to a floating-point power.
pub fn powi(self, n: i32) -> f32 pub fn powf(self, n: f32) -> f32
In Python we use
// to find a floor division. For example