Control Flow
if – no parentheses
In Rust, if does not take parentheses around the condition:
let n = 5;
if n < 10 {
println!("small");
} else if n < 20 {
println!("medium");
} else {
println!("large");
}In C#: Same logic, but C# uses if (n < 10). In Rust the condition must be a bool (no integer or pointer in condition).
if is an expression
if/else produces a value; you can assign from it:
let n = 5;
let size = if n < 10 { "small" } else { "large" };In C#: Like the ternary operator: var size = n < 10 ? "small" : "large";. In Rust, both branches must have the same type.
loop, break, continue
Infinite loop with explicit exit:
let mut count = 0;
loop {
count += 1;
if count >= 5 {
break;
}
}
// break can return a value: let x = loop { break 42; };In C#: Like while (true) with break/continue. Rust’s loop can return a value via break value.
while
let mut n = 3;
while n > 0 {
println!("{}", n);
n -= 1;
}In C#: Same as while (n > 0) { ... }.
for
Rust’s for always iterates over a range or iterator:
for i in 0..5 {
println!("{}", i); // 0, 1, 2, 3, 4
}
for i in 0..=5 {
println!("{}", i); // 0, 1, 2, 3, 4, 5
}In C#: for (int i = 0; i < 5; i++) or foreach (var i in Enumerable.Range(0, 5)). Rust has no C-style for (;;); use for over ranges or iterators.
Key takeaway
if has no parentheses and is an expression. Use loop/while/for; for is over ranges or iterators, not a C-style triple for.