Numeric Literals
Boolean and Character Literals
String Literals
Arithmetic Operators
Comparison Operators
Logical Operators
Bitwise Operators
Assignment Operators
Programming example on literals
Programming example on Alias
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
Programming example on literals
Integer literals (decimal, hex, binary, underscore)
fn main() {
let decimal = 255;
let hex = 0xff;
let binary = 0b1111_1111;
println!("decimal = {}", decimal);
println!("hex = {}", hex);
println!("binary = {}", binary);
}
Output
decimal = 255
hex = 255
binary = 255
2️⃣ Floating-point literals
fn main() {
let pi = 3.14159;
let scientific = 1.2e3;
println!("pi = {}", pi);
println!("scientific = {}", scientific);
}
Output
pi = 3.14159
scientific = 1200
3️⃣ Character and string literals
fn main() {
let ch = '₹';
let text = "Rust Language";
println!("character = {}", ch);
println!("string = {}", text);
}
Output
character = ₹
string = Rust Language
4️⃣ Boolean and tuple literals
fn main() {
let flag = true;
let point = (10, 20);
println!("flag = {}", flag);
println!("point = {:?}", point);
}
Output
flag = true
point = (10, 20)
5️⃣ Array and struct literals
struct User {
name: &'static str,
active: bool,
}
fn main() {
let nums = [1, 2, 3, 4];
let user = User {
name: "Ashwani",
active: true,
};
println!("nums = {:?}", nums);
println!("user = {} active={}", user.name, user.active);
}
Output
nums = [1, 2, 3, 4]
user = Ashwani active=true
Programming example on Alias
Immutable aliasing (multiple read-only references)
fn main() {
let x = 10;
let r1 = &x;
let r2 = &x; // allowed: multiple immutable references
println!("r1 = {}", r1);
println!("r2 = {}", r2);
}
Output
r1 = 10
r2 = 10
🔑 Rule
✅ Many immutable references (&T) are allowed at the same time.
2️⃣ Mutable aliasing (only ONE mutable reference)
fn main() {
let mut x = 5;
let r = &mut x; // only one mutable reference allowed
*r += 10;
println!("x = {}", x);
}
Output
x = 15
🔑 Rule
✅ Only one mutable reference (&mut T) at a time.
3️⃣ ❌ Invalid aliasing (mutable + immutable together)
fn main() {
let mut x = 20;
let r1 = &x;
// let r2 = &mut x; // ❌ compile-time error
println!("r1 = {}", r1);
}
Output
r1 = 20
❌ Why error if uncommented?
Rust forbids:
&x and &mut x at the same time
🔑 Rule
❌ You cannot mix mutable and immutable references.
4️⃣ Aliasing ends when scope ends
fn main() {
let mut x = 100;
{
let r = &x; // immutable alias
println!("inner r = {}", r);
} // r goes out of scope here
let m = &mut x; // now allowed
*m += 50;
println!("x = {}", x);
}
Output
inner r = 100
x = 150
🔑 Key Insight
Aliasing rules are scope-based.
5️⃣ Aliasing with slices (shared view of data)
fn main() {
let data = [1, 2, 3, 4];
let s1 = &data[0..2];
let s2 = &data[1..4]; // overlapping immutable slices allowed
println!("s1 = {:?}", s1);
println!("s2 = {:?}", s2);
}
Output
s1 = [1, 2]
s2 = [2, 3, 4]
🔑 Rule
✅ Multiple immutable slice aliases are allowed.
Top comments (0)