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

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

#11 Modern C++ and Rust Programming – A Comparative Educational Guide from Concepts to Applications

#11 Modern C++ and Rust Programming – A Comparative Educational Guide from Concepts to Applications

 

Data Types and Variables

Primitive Types: int, float, bool

Introduction to Primitive Types

Primitive types are the fundamental data types provided by a programming language to represent basic values. They form the building blocks of all complex data structures and variables. This section compares three core primitive types — int, float, and bool — as implemented in Modern C++ and Rust, highlighting their characteristics, ranges, and usage.

Integer Types (int)

C++

  • In C++, the keyword int refers to a signed integer type whose size is implementation-dependent but typically 32 bits on modern desktop and server platforms. The C++ standard guarantees a minimum size of 16 bits (ISO C++ Standard, latest draft, cppreference).

  • C++ provides a family of integer types:

    • Signed: int, short, long, long long

    • Unsigned: unsigned int, etc.

    • The exact size and ranges can be checked using <climits>, e.g., INT_MAX (cppreference limits).

  • Modern C++ (C++11 and later) offers fixed-width integer types via <cstdint> such as int32_t and uint64_t for guaranteed sizes across platforms (ISO C++11 Standard).

  • Typical range of a 32-bit int: −2,147,483,648 to 2,147,483,647.

Rust

  • Rust has explicitly sized integer types such as i32, i64, u32, u64 reflecting the number of bits and signedness. The int keyword does not exist in Rust, but isize and usize represent pointer-sized signed and unsigned integers respectively, varying by platform (Rust Reference).

  • Default integer literals without suffix default to i32 if type inference is needed (Rust book).

  • Rust integers provide defined overflow behavior in debug builds (panic on overflow) and wrapping behavior in release builds, improving safety and performance tradeoffs (Rust Overflow documentation).

  • Rust enforces explicit casting between integer types, reducing implicit conversion errors common in C++ (Rust casting).

Floating-Point Types (float)

C++

  • C++ provides floating-point types as per IEEE-754 standards: float (single precision), double (double precision), and long double (extended precision depending on the platform) (IEEE-754, cppreference).

  • float typically represents a 32-bit single precision floating-point number, with approximately 7 decimal digits of precision and exponent range of ±38 (IEEE-754 Single Precision).

  • C++ supports floating-point literals and provides functions and constants via <cmath> and <limits>.

  • C++20 introduced new features improving floating-point support and constexpr capabilities (ISO C++20 Standard).

Rust

  • Rust provides f32 and f64 as IEEE-754 compliant single and double precision floating-point types, respectively (Rust Reference).

  • Like integers, floating-point literals default to f64 unless otherwise specified (Rust book).

  • Rust’s standard library offers methods for floating-point arithmetic, comparison, and manipulation through inherent methods and traits (Rust std::f32).

  • Rust emphasizes safe floating-point usage with no implicit coercions and explicit handling of NaN, infinity, and precision limits (Rust RFC 1665).

Boolean Types (bool)

C++

  • The C++ bool type represents Boolean values: true or false. It was formally introduced in C++98, distinct from integer types (cppreference).

  • bool occupies 1 byte typically, but its exact size is implementation-defined.

  • Conversion rules allow implicit conversion between bool and integers (true → 1, false → 0), which sometimes leads to subtle bugs (Herb Sutter, C++ Core Guidelines).

Rust

  • Rust has a dedicated bool type representing two values: true and false.

  • Unlike C++, Rust does not allow implicit conversions between bool and integers, enforcing stronger type safety and preventing common bugs (Rust Reference).

  • The bool type occupies 1 byte.

Summary Table

AspectC++Rust
Integer typeint (platform dependent size), fixed-width via <cstdint>Explicit sized: i32, u64, isize, usize
Integer defaultNo literal default; type inferred or specifiedInteger literals default to i32
Integer overflowUndefined behavior or wrapping (UB in release)Panic on overflow in debug, wrapping in release
Float typefloat (32-bit), double (64-bit), long double (platform-dependent)f32 (32-bit), f64 (64-bit)
Float defaultDepends on context, usually doublef64 default for float literals
Boolean typebool, implicit int conversions allowedbool, no implicit conversions
Memory sizeVaries, often 4 bytes for int, 1 byte for boolExplicit sizes; predictable

References

  1. ISO C++ Standard latest drafts https://isocpp.org/std/the-standard

  2. C++ Primitive Types — cppreference https://en.cppreference.com/w/cpp/language/types

  3. C++ Fixed-Width Integer Types https://en.cppreference.com/w/cpp/types/integral_types

  4. IEEE-754 Standard overview https://ieeexplore.ieee.org/document/8766229

  5. Rust Reference — Numeric Types https://doc.rust-lang.org/reference/types/numeric.html

  6. Rust Book — Data Types https://doc.rust-lang.org/book/ch03-02-data-types.html

  7. Rust Reference — Boolean Type https://doc.rust-lang.org/reference/types/bool.html

  8. Herb Sutter’s C++ Core Guidelines (Logic Rules) https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rr-logic

  9. Rust RFC 1665: Float casts https://rust-lang.github.io/rfcs/1665-float-casts.html

Advertisements

Responsive Counter
General Counter
1000738
Daily Counter
2358