Skip to content

Instantly share code, notes, and snippets.

@dneto
Created Mar 31, 2012
Embed
What would you like to do?
Factorial recursive algorithm in Rust language
fn factorial(value: int) -> int {
if value==0 { ret 1; }
else { ret value * factorial(value-1); }
}
fn main(args: [str]) {
let init_value = option::get(int::from_str(args[1]));
let final_value = int::str(factorial(init_value));
io::println(final_value);
}
@Z34O
Copy link

Z34O commented May 23, 2017

Looks like Java, It won't work...
Change:
-int to i32
-remove args:[str]
-Why you need to do io::println? It's supposed to be "println!"

Copy link

ghost commented May 24, 2018

My dude this is a Gist from 2012.

@codedcosmos
Copy link

codedcosmos commented Jan 6, 2021

Just in-case (noone wanted this) anyone wanted it updated to 2020

fn factorial(x: i32) -> i32 {
    if x == 0 {
        1
    } else {
        x * factorial(x-1)
    }
}

@dneto
Copy link
Author

dneto commented Jan 6, 2021

Yes, this is a code from, maybe, alpha or pre-alpha rust. I was just trying to learn it.

The language changed over the time until the initial version was released, so this code doesn't run anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment