Created
November 26, 2023 18:48
-
-
Save i-asimkhan/e04e984a4ef550b6be52c8fb6f5104dc to your computer and use it in GitHub Desktop.
List of operators in #rust programming language
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[allow(dead_code)] | |
#[allow(unused_variables)] | |
fn main() { | |
operators(); | |
bitwise_operators(); | |
logical_operators(); | |
} | |
fn operators() { | |
// arithemetic operators | |
let mut result = 2+3*4; // +-*/ | |
println!("{}", result); | |
result = result +1; // missing -- and ++ | |
// however it does have -=, +=, /=, %=, *= | |
result -= 2; // result = result - 2 | |
println!("{}", result); | |
println!("Remainder of {} / {} = {}", result, 3, (result%3)); | |
let result_cubed = i32::pow(result, 3); | |
println!("{}^{} is {}", result,3 ,result_cubed); | |
let result_f = 2.5; | |
let result_f_cubed = f64::powi(result_f, 3); | |
let result_f_cubed_p1 = f64::powf(result_f, std::f64::consts::PI); | |
println!("{}^3 = {} and, {}^p1 = {}", result_f, result_f_cubed, result_f, result_f_cubed_p1); | |
} | |
fn bitwise_operators() { | |
// bitwise operatros | |
// | OR, & AND, ^ XOR, ! NOR | |
println!("1|2 = {}", 1|2); | |
println!("1&2 = {}", 1&2); | |
println!("1^2 = {}", 1^2); | |
println!("!2 = {}", !2); | |
// shift operators | |
println!("1<<2 = {}", 1<<2); // shift left | |
println!("1>>2 = {}", 1>>2); // shift right | |
println!("1>>2 = {}", 1 >> 2); // shift right to the zero | |
} | |
fn logical_operators() { | |
// logical | |
let pi_less_than_4 = std::f64::consts::PI < 4.0; // true | |
println!("Is {} is less than 4 ? {}", std::f64::consts::PI ,pi_less_than_4); | |
let x = 5; | |
let x_is_5 = x == 5; // true | |
println!("x = {}, is actually == {}, (so x is {}) is {}", x, x, x, x_is_5); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment