Starting with a new programming language is always hard. I decided to give me some exercises to get a grip of the language.

My very first rust programm is a simple clone of cat.

Assignment

Write a program in rust that prints the contents of a file to stdout.

Solution

Below is the solution I came up with. Can’t tell if its good or not at this very moment, but at least it compiles and runs.

use std::env;
use std::io::prelude::*;
use std::fs::File;

fn main() {
    let args = env::args();
    let file = args.skip(1).next().unwrap();

    let mut fh = File::open(file).unwrap(); 
    let mut buf = String::new();

    fh.read_to_string(&mut buf).unwrap();

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

What I learned

Learning exception handling in Rust was probably the difficult part. It did not expect to get confronted with it early.

Other notable things:

  • std::env
  • basic io
  • [E0308]
  • Looking up Rust on SO, because I had no clue what is happening