Function

C++:

#include <iostream>

using namespace std;

int sum(int x, int y)
{
    return x + y;
}

void print(x)
{
    cout << x << endl;
}

int main()
{
    int x = 1;
    int y = 2;
    int z = sum(x,y);
    print(z);
    return 0;
}

Rust :

fn sum(x : i32, y : i32) -> i32
{
    x + y
}

fn print(x : i32)
{
    println!("{}", x);
}

fn main()
{
    let x = 1;
    let y = 2;
    let z = sum(x, y);
    print(z);
}

Unlike C++, Rust declares data type after each variable in function signature. The return keyword can also be removed if the semicolon at the end of the return statement is not present, which is similar to some languages such as Ruby or Erlang

results matching ""

    No results matching ""