SimplifyC++ Article
#14 Modern C++ and Rust Programming – A Comparative Educational Guide from Concepts to Applications
#14 Modern C++ and Rust Programming – A Comparative Educational Guide from Concepts to Applications
Control Flow
Conditional Statements: if, else, switch
Introduction to Conditional Statements
Conditional statements allow programs to execute different blocks of code based on boolean expressions. They form the foundation of decision-making in programming. Both C++ and Rust provide similar constructs but with important syntactic and semantic differences reflecting their language philosophies.
if and else Statements
C++
The
ifstatement evaluates a condition; if the condition is true (non-zero), the following block executes; otherwise, an optionalelseblock executes (cppreference).Syntax:
if (condition) {// code if condition is true} else {// code if condition is false}Conditions must be convertible to
bool; implicit conversion from integral or pointer types is allowed. Zero ornullptrevaluates as false, non-zero as true.Nested
if-elseandelse ifchains are common.Since C++17,
ifstatements can include an initializer, introducing a new variable with limited scope:if (int x = foo(); x > 0) {// use x here}This pattern enhances code clarity and limits variable scope to the
ifblock (cppreference if statement).
Rust
In Rust,
ifis an expression and must evaluate to a boolean (bool) — no implicit conversion from integers or other types is allowed (Rust Reference).Syntax:
if condition {// code if true} else {// code if false}Because
ifis an expression, it returns a value, allowing:let x = if condition { 5 } else { 10 };There is no traditional ternary operator in Rust (
?:in C++); instead,ifexpressions serve this role.Rust requires explicit boolean conditions, enhancing type safety and reducing bugs.
The switch Statement
C++
The
switchstatement allows multi-way branching based on integral or enumeration types (cppreference).Syntax:
switch (expression) {case value1:// codebreak;case value2:// codebreak;default:// code}Important features:
switchworks only with integral, enumeration, orconstexprvalues convertible to integral types.Each
caselabel must be a compile-time constant.Fallthrough between cases occurs unless explicitly broken with
break.Since C++17,
[[fallthrough]];attribute can document intentional fallthrough.
switchprovides efficient jump table or binary search implementations by compilers.
Rust
Rust does not have a
switchstatement. Instead, it provides a more powerfulmatchexpression (Rust Reference).matchallows pattern matching on values of many types, not just integers.Syntax:
match value {pattern1 => { /* code */ },pattern2 => { /* code */ },_ => { /* default case */ },}Features:
Exhaustiveness checking: all possible cases must be handled or covered by a wildcard
_.Patterns can be literals, ranges, enums, or destructured data.
matchis an expression and returns a value.Prevents bugs common in
switch, such as missing cases or accidental fallthrough.
Summary of Differences
| Feature | C++ (if, else, switch) | Rust (if, else, match) |
|---|---|---|
| Condition type | Implicit conversions to bool allowed | Requires explicit bool condition |
if as expression | No (statement only) | Yes (returns value) |
| Ternary operator | Yes, ?: | No ternary; use if expressions |
| Multi-branching | switch supports integral/enums only | match supports pattern matching on many types |
| Fallthrough | Allowed by default; must use break to prevent | No fallthrough; exhaustive match required |
| Exhaustiveness | No compiler checks for missing cases | Compiler enforces exhaustive pattern matching |
| Variable binding | C++17 allows initializer in if | if and match allow variable bindings in patterns |
Practical Notes and Best Practices
Use
ifandelsefor simple conditional branches in both languages.Prefer
switchin C++ for multiple discrete integer or enum cases with care forbreakstatements.Use Rust’s
matchfor powerful, safe, and exhaustive multi-way branching that can destructure complex data types.Exploit C++17's
ifinitializer to limit scope of variables used in conditions.Rust’s strict boolean conditions and exhaustive matching reduce runtime errors and improve code safety.
References
C++
ifstatement — cppreference https://en.cppreference.com/w/cpp/language/ifC++
switchstatement — cppreference https://en.cppreference.com/w/cpp/language/switchISO C++17 Standard (for
ifinitializer and[[fallthrough]]) https://isocpp.org/std/the-standardRust
ifexpression — Rust Reference https://doc.rust-lang.org/reference/expressions/if-expr.htmlRust
matchexpression — Rust Reference https://doc.rust-lang.org/reference/expressions/match-expr.htmlRust Book: Control Flow https://doc.rust-lang.org/book/ch03-05-control-flow.html