Rust: fib

Rust is an interesting language. It is not a primitive one, like Go where we don't have ADTs, pattern matching and generics (but we do have Nils). And it's advertising as a safe and performant system language. Today is the very first day I'm looking at it. Let's "smoke" test it with Fibonacci :)

extern crate time;
use time::now;
fn fib(n: i32) -> i32 {
match n {
0 | 1 => n,
_ => fib (n-1) + fib(n-2)
}
}
fn main() {
let start = now();
for n in 0..40 {
println!("fib ({}) = {}", n, fib(n));
}
println!("Done in {}", now() - start);
}
view raw fib.rs hosted with ❤ by GitHub

Debug: 3.44 seconds, release: 1.66 seconds. This is not very impressive, but pretty fast indeed.
  • C# - 1.26
  • D (DMD) - 1.3
  • F# - 1.38
  • Nemerle - 1.45
  • Rust - 1.66
  • Haskell - 2.8
  • Clojure - 9
  • Erlang - 17
  • Ruby - 60
  • Python - 120
It's very interesting how it'll behave in concurrent Fibonacci test.

The compiler is quite slow: it takes 2-3 seconds to build this tiny program.

Comments

Popular posts from this blog

Regular expressions: Rust vs F# vs Scala

Hash maps: Rust, F#, D, Go, Scala

Haskell: performance