Numeric Literals
Boolean and Character Literals
String Literals
Arithmetic Operators
Comparison Operators
Logical Operators
Bitwise Operators
Assignment Operators
Numeric Literals
fn main() {
let a = 10; // integer literal
let b = 3.14; // floating-point literal
let c = 1_000_000; // underscore for readability
let d = 0xff; // hexadecimal literal
let e = 0b1010; // binary literal
println!("{}", a);
println!("{}", b);
println!("{}", c);
println!("{}", d);
println!("{}", e);
}
✅ Output
10
3.14
1000000
255
10
Boolean and Character Literals
fn main() {
let is_active = true;
let is_admin = false;
let grade = 'A';
println!("{}", is_active);
println!("{}", is_admin);
println!("{}", grade);
}
✅ Output
true
false
A
String Literals
fn main() {
let name = "Ashwani";
let message = "Welcome to Rust!";
println!("{}", name);
println!("{}", message);
}
✅ Output
Ashwani
Welcome to Rust!
Arithmetic Operators
fn main() {
let a = 10;
let b = 3;
println!("{}", a + b); // addition
println!("{}", a - b); // subtraction
println!("{}", a * b); // multiplication
println!("{}", a / b); // division
println!("{}", a % b); // modulus
}
✅ Output
13
7
30
3
1
Comparison Operators
fn main() {
let x = 10;
let y = 20;
println!("{}", x == y);
println!("{}", x != y);
println!("{}", x < y);
println!("{}", x > y);
println!("{}", x <= y);
println!("{}", x >= y);
}
✅ Output
false
true
true
false
true
false
Logical Operators
fn main() {
let a = true;
let b = false;
println!("{}", a && b);
println!("{}", a || b);
println!("{}", !a);
}
✅ Output
false
true
false
Bitwise Operators
fn main() {
let x = 5; // 0101
let y = 3; // 0011
println!("{}", x & y); // AND
println!("{}", x | y); // OR
println!("{}", x ^ y); // XOR
println!("{}", x << 1); // Left shift
println!("{}", x >> 1); // Right shift
}
✅ Output
1
7
6
10
2
Assignment Operators
fn main() {
let mut x = 10;
x += 5;
println!("{}", x);
x *= 2;
println!("{}", x);
x -= 4;
println!("{}", x);
}
✅ Output
15
30
26
Combined Literal + Operator Example
fn main() {
let radius = 7.0;
let pi = 3.14159;
let area = pi * radius * radius;
println!("Area = {:.2}", area);
}
✅ Output
Area = 153.94
Top comments (0)