Logo
Articles Compilers Libraries Books MiniBooklets Assembly C++ Linux Others Videos
Advertisement

Article by Ayman Alheraki on January 11 2026 10:37 AM

#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 if statement evaluates a condition; if the condition is true (non-zero), the following block executes; otherwise, an optional else block executes (cppreference).

  • Syntax:

  • Conditions must be convertible to bool; implicit conversion from integral or pointer types is allowed. Zero or nullptr evaluates as false, non-zero as true.

  • Nested if-else and else if chains are common.

  • Since C++17, if statements can include an initializer, introducing a new variable with limited scope:

  • This pattern enhances code clarity and limits variable scope to the if block (cppreference if statement).

Rust

  • In Rust, if is an expression and must evaluate to a boolean (bool) — no implicit conversion from integers or other types is allowed (Rust Reference).

  • Syntax:

  • Because if is an expression, it returns a value, allowing:

  • There is no traditional ternary operator in Rust (?: in C++); instead, if expressions serve this role.

  • Rust requires explicit boolean conditions, enhancing type safety and reducing bugs.

The switch Statement

C++

  • The switch statement allows multi-way branching based on integral or enumeration types (cppreference).

  • Syntax:

  • Important features:

    • switch works only with integral, enumeration, or constexpr values convertible to integral types.

    • Each case label must be a compile-time constant.

    • Fallthrough between cases occurs unless explicitly broken with break.

    • Since C++17, [[fallthrough]]; attribute can document intentional fallthrough.

  • switch provides efficient jump table or binary search implementations by compilers.

Rust

  • Rust does not have a switch statement. Instead, it provides a more powerful match expression (Rust Reference).

  • match allows pattern matching on values of many types, not just integers.

  • Syntax:

  • Features:

    • Exhaustiveness checking: all possible cases must be handled or covered by a wildcard _.

    • Patterns can be literals, ranges, enums, or destructured data.

    • match is an expression and returns a value.

    • Prevents bugs common in switch, such as missing cases or accidental fallthrough.

Summary of Differences

FeatureC++ (if, else, switch)Rust (if, else, match)
Condition typeImplicit conversions to bool allowedRequires explicit bool condition
if as expressionNo (statement only)Yes (returns value)
Ternary operatorYes, ?:No ternary; use if expressions
Multi-branchingswitch supports integral/enums onlymatch supports pattern matching on many types
FallthroughAllowed by default; must use break to preventNo fallthrough; exhaustive match required
ExhaustivenessNo compiler checks for missing casesCompiler enforces exhaustive pattern matching
Variable bindingC++17 allows initializer in ifif and match allow variable bindings in patterns

 

Practical Notes and Best Practices

  • Use if and else for simple conditional branches in both languages.

  • Prefer switch in C++ for multiple discrete integer or enum cases with care for break statements.

  • Use Rust’s match for powerful, safe, and exhaustive multi-way branching that can destructure complex data types.

  • Exploit C++17's if initializer to limit scope of variables used in conditions.

  • Rust’s strict boolean conditions and exhaustive matching reduce runtime errors and improve code safety.

References

  1. C++ if statement — cppreference https://en.cppreference.com/w/cpp/language/if

  2. C++ switch statement — cppreference https://en.cppreference.com/w/cpp/language/switch

  3. ISO C++17 Standard (for if initializer and [[fallthrough]]) https://isocpp.org/std/the-standard

  4. Rust if expression — Rust Reference https://doc.rust-lang.org/reference/expressions/if-expr.html

  5. Rust match expression — Rust Reference https://doc.rust-lang.org/reference/expressions/match-expr.html

  6. Rust Book: Control Flow https://doc.rust-lang.org/book/ch03-05-control-flow.html

Advertisements

Responsive Counter
General Counter
1000767
Daily Counter
2387