ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules

How to write Rust solutions

Rust programs are compiled using rustc compiler version 1.75.0 x64 with the following parameters:

rustc -O %1

You can get the latest Rust compiler from the official website.

Examples of solving a problem

A sample solution for the 1000. A + B problem in Rust:

use std::io;

fn main() {
    let mut input = String::new();

    io::stdin()
        .read_line(&mut input)
        .unwrap();

    let mut s = input.trim().split(' ');

    let a_str = s.next().unwrap();
    let a: i32 = a_str.parse().unwrap();

    let b_str = s.next().unwrap();
    let b: i32 = b_str.parse().unwrap();

    println!("{}", a + b);
}

Or more advanced and effective solution for the same problem:

use std::io;

fn main() {
    let mut input = String::new();

    io::stdin()
        .read_line(&mut input)
        .expect("correct input");
    let res = input.trim()
                   .split(' ')
                   .map(|a| a.parse::<i32>())
                   .map(|a| a.expect("parsed integer"))
                   .fold(0i32, |sum, a| sum + a);

    println!("{}", res);
}

A sample solution for the 1001. Reverse Root in Rust:

use std::io;
use std::io::prelude::*;

fn main() {
    let mut v: Vec<i64> = Vec::new();
    let stdin = io::stdin();

    for t in stdin.lock().lines() {
        let cur_line = t.unwrap();
        for u in cur_line.split(' ') {
            let cur_txt = u.trim();
            if cur_txt.len() > 0 {
                let cur_num: i64 = cur_txt.parse().unwrap();
                v.push(cur_num);
            }
        }
    }
    let mut i = v.len();
    while i > 0 {
        i -= 1;
        let f_num = v[i] as f64;
        println!("{:.*}", 4, f_num.sqrt());
    }
}

Or more advanced and effective solution for the same problem:

use std::io::{self, BufReader};
use std::io::prelude::*;

fn solve(input: &mut Read, output: &mut Write) {
    let reader = BufReader::new(input);
    let mut v: Vec<i64> = Vec::new();

    for t in reader.lines()
                   .map(|a| a.expect("correct input")) {
        for u in t.split(' ')
                  .map(|a| a.trim())
                  .filter(|a| a.len() > 0)
                  .map(|a| a.parse::<i64>().expect("parsed integer")) {
            v.push(u);
        }
    }

    for u in v.into_iter().rev().map(|a| a as f64) {
        writeln!(output, "{:.*}", 4, u.sqrt()).expect("valid output");
    }
}

fn main() {
    solve(&mut io::stdin(), &mut io::stdout());
}

Tests

Rust has a built-in engine for unit tests. One can write a test section using a special #[test] directive and it will be compiled only if a special option is defined for the compiler:

rustc --test main.rs

Here is an example of reading a test data from a file (for the problem 1001 solution above):

fn solve(input: &mut Read, output: &mut Write) {
    ...
}

fn main() {
    solve(&mut io::stdin(), &mut io::stdout());
}

#[cfg(test)]
mod tests {
    use std::fs::File;
    use solve;

    #[test]
    fn basic_test() {
        let mut f = File::open("test1.txt").expect("correct test");
        let mut buf: Vec<u8> = Vec::new();

        solve(&mut f, &mut buf);

        let res = String::from_utf8(buf).expect("valid string");
        assert_eq!(res,
"2297.0716
936297014.1164
0.0000
37.7757
");
    }
}

Earlier compilers

  • The Rust 1.9.0 x86 was used until April, 12 2018.
  • The Rust 1.25.0 x86 was used until September, 1 2020.
  • The Rust 1.45.0 x64 was used until January, 22 2022.
  • The Rust 1.58.1 x64 was used until January, 22 2024.