Skip to content

Instantly share code, notes, and snippets.

@dneto
Created March 31, 2012 01:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dneto/2258454 to your computer and use it in GitHub Desktop.
Save dneto/2258454 to your computer and use it in GitHub Desktop.
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);
}
@brylleee
Copy link

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

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