Adding time and rand crates for Rust

I would be nice to have the crates (libraries) time and rand for the Rust programming language as there is currently no way of getting the time or generating random numbers using Rust (except writing the foreign functions interface to libc).
This would allow using more advanced strategies more easily.
Thanks for your amazing work!



I 'm learning rust with codinGame. I do puzzle, and bot in Scala or Rust. But use Scala for puzzle, bot that require random.

Also, can you ugrade to rust 1.7.0 (March 3, 2016) ?

Thanks for your great site.

1 Like

Same here, I learn Rust through puzzles now, and I find this site really helpful. But for the coming contest, measuring the time can be crucial.
Thanks for your efforts!

1 Like


Not being able to do random for rust user is a handicap for contest.

Any update from CG team

Hi !

Same problem here. rust-1.8 is now out but I understand that’s a lot of work for CG team.

Here is a workaround for rand:

fn urandom() -> u8 {
    let mut f = File::open("/dev/urandom").unwrap();
    let mut buffer = [0; 1]; buffer).unwrap();
    return buffer[0];

No real solution for time as FFI is not allowed in current version (1.6.0)
Checkout 1.6.0 Rust Documentation

Rust updated to 1.8.

Sorry we couldn’t upgrade Rust during the contest but now it’s done. Regarding time and rand we need to compile using cargo instead of rustc. We don’t have the time this week but hopefully this should not be technically too difficult to implement.

I’m not sure if this is the right place but is Rust compiled in release mode? cargo run --release?

We don’t use cargo for the moment, so no.

Fair enough. Do you run rustc with optimizations on then? It might be nice to list the actual compile commands for each of the languages in the faq.

I don’t think there is any language compiled with optimization here.

In the last challenge and when I did some puzzles in Rust, I had to avoid using iterators and vector because they are too slow without optimization.

Thanks a lot for having updated Rust to 1.8 :smile:

In fact, C++ dev can use “#pragma GCC Optimize”, it provides a big plus vs other language (like Rust) that are not compiled with release or optimization flag, when you have to do computation, …

1 Like


Any news from the CodinGame team on the subject?

I would really appreciate to use time and rand crates. And it would be even better if we can have optimized compilations also.

Thank you!

Currently, we are using rustc to compile your source code. I wanted to use cargo instead to allow the use of the rand crate but the compilation time went from 0.15 seconds to 2 seconds to compile a simple hello world… 2 seconds is a problem for the clashes…

If you have an idea of to compile with time and rand in less than half a second, I’m all ears :).

1 Like

Thanks @MaximeC ,

I did some test, I didn’t have the same result, but the same fact cargo is longer if it do everything (including download and build crates/lib).

If you’re ok to keep the compiled crates in the image (docker, ABI,… what you use) I should have a solution:

  1. use cargo to download the crates and store them on the image
  2. use rustc with thoses downloaded files.
    I could upload the project on github
➜  codingame cargo new cg_rust --bin

➜  codingame cd cg_rust

➜  cg_rust git:(master) ✗ cat >>Cargo.toml <<EOF
time = "0.1"
rand = "0.3"

➜  cg_rust git:(master) ✗ time cargo build
    Updating registry ``
   Compiling libc v0.2.16
   Compiling winapi-build v0.1.1
   Compiling winapi v0.2.8
   Compiling kernel32-sys v0.2.2
   Compiling rand v0.3.14
   Compiling time v0.1.35
   Compiling cg_rust v0.1.0 (file:///Users/davidb/share/codingame/cg_rust)
cargo build  4.11s user 0.92s system 40% cpu 12.366 total

➜  cg_rust git:(master) ✗ cat >src/ <<EOF
extern crate time;
extern crate rand;
use rand::random;

fn main() {
    let t0 = time::precise_time_ns();
    let r: i32 = random::<i32>();
    println!("Hello, world! time: {} random: {}", t0, r);

➜  cg_rust git:(master) ✗ time cargo build
   Compiling cg_rust v0.1.0 (file:///Users/davidb/share/codingame/cg_rust)
cargo build  0.15s user 0.06s system 95% cpu 0.227 total

➜  cg_rust git:(master) ✗ time cargo run
     Running `target/debug/cg_rust`
Hello, world! time: 227654162133799 random: -1360491862
cargo run  0.04s user 0.02s system 93% cpu 0.073 total

➜  cg_rust git:(master) ✗ time target/debug/cg_rust
Hello, world! time: 227670286303399 random: 1928059675
target/debug/cg_rust  0.00s user 0.00s system 69% cpu 0.004 total

➜  cg_rust git:(master) ✗ time rustc src/ --extern rand=target/debug/deps/librand-6fab17fc670a2bc8.rlib -L target/debug/deps -O
rustc src/ --extern  -L target/debug/deps -O  0.10s user 0.03s system 77% cpu 0.170 total

➜  cg_rust git:(master) ✗ time ./main
Hello, world! time: 227722446534341 random: -1796789735
./main  0.00s user 0.00s system 71% cpu 0.004 total

So you could copy the target/debug/deps directory of the project and use it as dependencies lib.
Using rustc -L deps with this directory should be enough but it seems that rust© already include a rand carte (bug, so we have to explicitly define the “rand” crate with “–extern rand=…” (the hash number could be different in your environment).

Thanks again for your time, It would be awasome if you could include time and rand crates (and -O) for the next contest.

Sorry for the long post, I hope I give you all the info.

PS: as you can see, only override the into already pre-build cargo project could also be a solution, compile time are very similar between rustc (0.10s) and cargo build (0.15s)


Thank you for your message. I’ve put on hold my work on Rust because I needed to work on another project but hopefully I’ll be able to work on that soon.

Our current problem is that the container is reinitialized after each build+execution and as shown in your experiments, building with cargo for the first time is slow (4.11s user in your case).

I agree with your suggestion to keep the compiled crates on the system. Actually, didn’t have the time to investigate, but there might be a way to specify the path of the crates in the Cargo.toml file. But the solution using rustc would be fine too.


You can specify a path to the .rlib files using rustc:

rustc src/ -L dependency=/path/to/rand/target/debug/deps/ \
    --extern rand=/path/to/rand/target/debug/deps/librand.rlib
1 Like

Thanks david_bernard_31 & bouanto, I’ve added the rlibs using rustc.


Thanks for your efforts! Time measuring works fine now! Much better options for this contest. :slight_smile:

1 Like