nestedloop

Full source code

C++

// -*- mode: c++ -*-
// $Id$
// http://www.bagley.org/~doug/shootout/

#include <iostream>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[]) {
#ifdef SMALL_PROBLEM_SIZE
#define LENGTH 30
#else
#define LENGTH 46
#endif
    int n = ((argc == 2) ? atoi(argv[1]) : LENGTH);
    int a, b, c, d, e, f, x=0;
        
    for (a=0; a<n; a++)
        for (b=0; b<n; b++)
            for (c=0; c<n; c++)
                for (d=0; d<n; d++)
                    for (e=0; e<n; e++)
                        for (f=0; f<n; f++)
                            x++;

    cout << x << endl;
    return(0);
}

Rust

// Adapted from https://github.com/llvm/llvm-test-suite and
// http://www.bagley.org/~doug/shootout/
use std::env;

#[cfg(feature = "small_problem_size")]
const LENGTH: i32 = 30;

#[cfg(not(feature = "small_problem_size"))]
const LENGTH: i32 = 46;

fn main() {
    let mut args = env::args();
    let n = if args.len() == 2 {
        args.nth(1).unwrap().parse::<i32>().unwrap()
    } else {
        LENGTH
    };

    let mut x: i32 = 0;

    for _a in 0..n {
        for _b in 0..n {
            for _c in 0..n {
                for _d in 0..n {
                    for _e in 0..n {
                        for _f in 0..n {
                            x = x.wrapping_add(1);
                        }
                    }
                }
            }
        }
    }

    println!("{}", x);
}

Porting notes

Integer addition without trapping

C++

                            x++;

Rust

#![allow(unused)]
fn main() {
                            x = x.wrapping_add(1);
}

In Rust, the normal integer addition would panic if it causes an overflow, unlike in C++. To avoid this, the Rust version uses the wrapping_add method.