SimplifyC++ Article
9 Modern C++ and Rust Programming – A Comparative Educational Guide from Concepts to Applications
#9 Modern C++ and Rust Programming – A Comparative Educational Guide from Concepts to Applications
Your First Program
Hello World in both C++ and Rust
Introduction: The Traditional “Hello World”
The “Hello World” program is the canonical starting point for learning any programming language. It serves as a simple example to illustrate the basic syntax for outputting text to the console, and it often reveals fundamental language concepts like compilation, program structure, and standard library usage.
This section compares the “Hello World” program in both Modern C++ (using C++17/20 conventions) and Rust (edition 2021), providing insight into language syntax, compilation, and execution.
Hello World in Modern C++
int main() { std::cout << "Hello, World!" << std::endl; return 0;}
Explanation:
#include
: This directive includes the standard input/output stream library, which contains std::cout for console output (cppreference.com). int main(): The entry point of the C++ program, returning an integer status code to the operating system. return 0; conventionally means successful execution (ISO C++ Standard).
std::cout << "Hello, World!" << std::endl;: Streams the string literal "Hello, World!" to the standard output, followed by a newline (std::endl). The std:: prefix specifies the use of the standard namespace (cplusplus.com).
Compilation and Execution:
Compiled with a C++ compiler such as GCC, Clang, or MSVC using a command like:
fn main() { println!("Hello, World!");}
Modern C++ compilers fully support the standard library and optimizations, ensuring the program is efficient with minimal startup overhead (GCC 12 release notes).
Hello World in Rust
Explanation:
fn main(): The entry point of the Rust program, which returns the unit type () implicitly. Rust functions are declared with fn (Rust Reference).
println!("Hello, World!");: Macro that prints text to the console, automatically appending a newline. Macros in Rust use an exclamation mark !. The macro provides formatting support similar to printf but with compile-time checks (Rust Standard Library).
Compilation and Execution:
Compiled with the Rust compiler rustc:
rustc hello.rs./hello
The Rust compiler performs aggressive optimizations and ensures memory safety at compile time. The latest Rust 1.70 release (2023) continues to improve compile times and executable performance (Rust Blog).
Key Comparative Points
| Aspect | C++ “Hello World” | Rust “Hello World” |
|---|---|---|
| Program entry point | int main() with explicit return | fn main() returns unit type implicitly |
| Output syntax | Stream-based std::cout and<< operator | Macro-based println! with formatting |
| Header inclusion | Requires explicit #include | No header inclusion, core macros are built-in |
| Memory safety | Manual memory safety, not an issue for simple output | Guaranteed memory safety, even in complex programs |
| Compilation command | g++ -std=c++17 or equivalent | rustc |
| Language paradigm | Multi-paradigm: procedural, object-oriented, generic | Multi-paradigm: procedural, functional, ownership-based |
| Error handling | Return codes from main and exceptions | Implicit unit return; error handling via Result and macros |
References and Further Reading
C++ Standard Library – iostream
https://en.cppreference.com/w/cpp/header/iostream
Basic Input/Output in C++
http://www.cplusplus.com/doc/tutorial/basic_io/
ISO C++ Standard Documentation
https://isocpp.org/std/the-standard
GCC 12 Release Notes
Rust Reference: Functions
https://doc.rust-lang.org/reference/items/functions.html
Rust println! Macro
https://doc.rust-lang.org/std/macro.println.html
Rust 1.70 Release Notes
https://blog.rust-lang.org/2023/06/01/Rust-1.70.0.html
Rust Official Website
What did you think?
Sign in to react or comment.
Comments
0No comments yet.