Idioms and Patterns Story
Hello World
Home
Repo
prev
next
Top, Bottom
Hello world
Demonstration compares C#, C++, and Rust "Hello World" programs
Each item in the IdiomsAndPatterns repository focues on a simple program construct built
in each of the three languages: C++ 20, Rust 1.49, and C# 9.0. This may help you use what
you know of C++ or C# (or Java) to learn Rust more easily.
We start here with the simplest programs in each of the three languagues.
Find instructions for downloading build chains for each of these languages in Tooling.
C++
hide
// Hello.cpp
#include <iostream>
int main() {
std::cout << "\n Hello World from C++"
<< "\n\n";
}
C++ build and execute
cd build
C:\...\HelloWorld\Cpp\build>
cmake .. >NULL
C:\...\HelloWorld\Cpp\build>
cmake --build . >NULL
C:\...\HelloWorld\Cpp\build>
"./debug/HelloPrj.exe"
Hello World from C++
CMakeLists.txt
# CMake HelloPrj - build C++ with CMake
project(HelloPrj)
set(CMake_CXX_STANDARD 20)
# build HelloCMake.obj in folder
# build/debug
add_executable(HelloPrj Hello.cpp)
C++ Project Setup
Steps to create Project:
- Create directory for project
- Add source code file
- Add CmakeLists.txt
- CMakeDemo
- mkdir build
- cd build
- cmake .. >NULL
- cmake --build . >NULL
- "./debug/PrjName.exe"
Rust
unhide
// HelloWorld::main.rs
fn main() {
println!("Hello, world!");
}
Rust build and execute
C:\...\HelloWorld\Rust>
cargo run -q
Hello, world!
cargo.toml
[package]
name = "rust"
version = "0.1.0"
authors = ["James W. Fawcett "]
edition = "2018"
[dependencies]
Rust Project Setup
Steps to create Project:
- Issue command: cargo new PkgName
- Edit cargo.toml to provide
pkg_name in snake_case
- cd src
- Edit main.rs for project constent
- cargo run -q
C#
hide
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
C# build and execute
C:\...\HelloWorld\CSharp>
dotnet run
Hello World!
C# Project
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>
net5.0
</TargetFramework>
</PropertyGroup>
</Project>
C# Project Setup
Steps to create Project:
- Issue command: dotnet new console
- Edit Program.cs for project constent
- dotnet run
4. Epilogue
The following pages provide sequences of code examples for idioms and principles in each of the three languages
cited here, e.g. C#, C++, and Rust. Object model differences will often be pointed out in comments within the
code blocks.