C++ vs. Rust: A Comparative Guide to Syntax, Variables, and Control Flow

anooop102910

Anoop Singh

Posted on September 22, 2024

C++ vs. Rust: A Comparative Guide to Syntax, Variables, and Control Flow

Hello World

C++ Version:

#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Rust version:

fn main() {
    println!("Hello, World!");
}
Enter fullscreen mode Exit fullscreen mode

Variables and datatypes:

C++ version:

#include <iostream>

int main() {
    int x = 5;
    double y = 3.14;
    std::cout << "x: " << x << ", y: " << y << std::endl;
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Rust version :

fn main() {
    let x: i32 = 10; // here i32 is a 32-bit integer
    let y = 22; // Rust can infer the type from given value
    let z: f32 = 30.0;
    x = x + 1; // This will throw an error because x is immutable
}
Enter fullscreen mode Exit fullscreen mode

In rust variable are immutable by default

To make them mutable

fn main() {
    let mut x= 5;
    x = x + 1;
    println!("x: {}", x);
}
Enter fullscreen mode Exit fullscreen mode

But immutable doesnā€™t means they are constant -

fn main() {
    let x = 5;
    let x = 10; // You can shadow the variable
    println!("x: {}", x);
}
Enter fullscreen mode Exit fullscreen mode

Why need such functionality?

fn main() {
    let s = "hello ";
    let s = s.trim(); // No need to create a new variable
    println!("s: {}", s); 
}
Enter fullscreen mode Exit fullscreen mode

Functions:

C++ version

int add(int a, int b) {
    return a + b;
}

int main() {
    std::cout << "Sum: " << add(5, 3) << std::endl;
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Rust version

fn add(a: i32, b: i32) -> i32 { // Type required
    a + b // Note - semicolon is omitted   
}

fn main() {
    println!("Sum: {}", add(5, 3));
}
Enter fullscreen mode Exit fullscreen mode

Conditionals

C++ version

int main() {
    int number = 10;

    if (number > 5) {
        std::cout << "Number is greater than 5." << std::endl;
    } else if (number == 5) {
        std::cout << "Number is equal to 5." << std::endl;
    } else {
        std::cout << "Number is less than 5." << std::endl;
    }

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Rust version;

fn main() {
    let number = 10;

    if number > 5 {
        println!("Number is greater than 5.");
    } else if number == 5 {
        println!("Number is equal to 5.");
    } else {
        println!("Number is less than 5.");
    }
}
Enter fullscreen mode Exit fullscreen mode

Loops

C++ version

int main() {
    for (int i = 0; i < 5; ++i) {
        std::cout << "Iteration: " << i << std::endl;
    }

    int j = 0;
    while (j < 5) {
        std::cout << "While Iteration: " << j << std::endl;
        j++;
    }

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Rust version:

int main() {
    for i in 0..5 {
        println!("Iteration: {}", i);
    }

   let mut count = 0;
    loop {
        count += 1;
        println!("{}", count);
        if count > 10 {
            break; 
        }
    }

    let mut j = 0;
    while j < 5 {
        println!("While Iteration: {}", j);
        j += 1;
    }
}
Enter fullscreen mode Exit fullscreen mode
šŸ’– šŸ’Ŗ šŸ™… šŸš©
anooop102910
Anoop Singh

Posted on September 22, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related