Skip to content

Instantly share code, notes, and snippets.

@dneto

dneto/factorial.rs

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

This comment has been minimized.

Copy link

@Z34O 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!"

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented May 24, 2018

My dude this is a Gist from 2012.

@codedcosmos

This comment has been minimized.

Copy link

@codedcosmos 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

This comment has been minimized.

Copy link
Owner Author

@dneto 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