SimplifyC++ Article
#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
intrefers 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 longUnsigned:
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 asint32_tanduint64_tfor 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,u64reflecting the number of bits and signedness. Theintkeyword does not exist in Rust, butisizeandusizerepresent pointer-sized signed and unsigned integers respectively, varying by platform (Rust Reference).Default integer literals without suffix default to
i32if 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), andlong double(extended precision depending on the platform) (IEEE-754, cppreference).floattypically 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
f32andf64as IEEE-754 compliant single and double precision floating-point types, respectively (Rust Reference).Like integers, floating-point literals default to
f64unless 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++
booltype represents Boolean values:trueorfalse. It was formally introduced in C++98, distinct from integer types (cppreference).booloccupies 1 byte typically, but its exact size is implementation-defined.Conversion rules allow implicit conversion between
booland integers (true→ 1,false→ 0), which sometimes leads to subtle bugs (Herb Sutter, C++ Core Guidelines).
Rust
Rust has a dedicated
booltype representing two values:trueandfalse.Unlike C++, Rust does not allow implicit conversions between
booland integers, enforcing stronger type safety and preventing common bugs (Rust Reference).The
booltype occupies 1 byte.
Summary Table
| Aspect | C++ | Rust |
|---|---|---|
| Integer type | int (platform dependent size), fixed-width via <cstdint> | Explicit sized: i32, u64, isize, usize |
| Integer default | No literal default; type inferred or specified | Integer literals default to i32 |
| Integer overflow | Undefined behavior or wrapping (UB in release) | Panic on overflow in debug, wrapping in release |
| Float type | float (32-bit), double (64-bit), long double (platform-dependent) | f32 (32-bit), f64 (64-bit) |
| Float default | Depends on context, usually double | f64 default for float literals |
| Boolean type | bool, implicit int conversions allowed | bool, no implicit conversions |
| Memory size | Varies, often 4 bytes for int, 1 byte for bool | Explicit sizes; predictable |
References
ISO C++ Standard latest drafts https://isocpp.org/std/the-standard
C++ Primitive Types — cppreference https://en.cppreference.com/w/cpp/language/types
C++ Fixed-Width Integer Types https://en.cppreference.com/w/cpp/types/integral_types
IEEE-754 Standard overview https://ieeexplore.ieee.org/document/8766229
Rust Reference — Numeric Types https://doc.rust-lang.org/reference/types/numeric.html
Rust Book — Data Types https://doc.rust-lang.org/book/ch03-02-data-types.html
Rust Reference — Boolean Type https://doc.rust-lang.org/reference/types/bool.html
Herb Sutter’s C++ Core Guidelines (Logic Rules) https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rr-logic
Rust RFC 1665: Float casts https://rust-lang.github.io/rfcs/1665-float-casts.html