last update: 2017 08 23
  1. Installation of Rust

    For Windows 10.
    For Linux.
  2. Basic documentation

    Rust Documentation
    The Rust Programming Language
    Rust by Example
  3. Naming convention

    • Files, modules, functions, fields should have snake_case names.
    • Constants should have SNAKE_CASE names.
    • Types should have CamelCase names.
  4. Use cargo

    More information.

    Create the project folder

    For a library:
    cargo new hello_world

    For an executable binary:
    cargo new hello_world --bin

    Compile the project

    cargo build

    Compile the project and run the executable

    cargo run
  5. Modules

    A module is a namespace.
    More information.


    A source file corresponds to a module.
    The file name (without the .rs suffix) corresponds to the module name.


    To use the struct Time declared in module time:
    mod time; //This tells the compiler what module to use. The module of file in the same folder.
    use time::Time; //This tells the compiler what thing to use from the module time. In my case the struct Time.
    Not declaring the used things results in this error:
    error[E0531]: unresolved struct, variant or union type `Time`
  6. Structs

    How to declare a struct type


    struct Time {
        second: u64,
        fraction: u64,

    No default values for fields can be declared.

    How to create a new struct value

    let x = Time {second:0, fraction:0};

    • No new keyword.
    • Curly brackets.
    • All fields must be initialized using a colon.
  7. Public and private access

    Public things can be used in other modules.
    Private things can be used only in the same module.
    By default a thing is private.

    Public declaration

    By using the word pub.


    pub struct Time {
        pub second:u64,
        pub fraction:u64,

    Each member must be declared public in order to be public.

    Not declaring pub results in these errors when accessed from another module:
    error: struct `Time` is private
    error[E0451]: field `second` of struct `time::Time` is private
  8. Object oriented programming in Rust

    Use implementations and traits.
  9. Strings

    String and &str

    More information here and here.

    How to create a String value from a value

    By convention the to_string(&self) -> String method is used.

    How to create a &str value from a String value

    By using the sign &:

    How to create a String value from a &str value

    There are 2 ways:
    • strRef.to_string()
    • String::from(strRef)

    How to concatenate strings

    The complicated way:

    The simple way:
    string + &str + &str
    where string can be a value with type String, &String, str, &str.

    How to print strings

    println!("{}", strRef)
    println!("{}", stringValue)

    println!(strRef) or println!(stringValue) give this error:
    error: format argument must be a string literal.

    More information about the print macro.
  10. Error handling

    Option and Result

    Errors are typically expressed and handled by using the types:

    Use of Option values

    match option {
        None => None,
        Some(value) => value,

    Use of Result values

    match result {
        Result::Ok(val) => val,
        Result::Err(err) => panic!("called `Result::unwrap()` on an `Err` value: {:?}", err),

    The try! macro

    The try! macro is an abbreviation for a certain match of Result values.

    is roughly but not exactly equivalent to:
    match (result) {
        Ok(val) => val,
        Err(err) => return Err(err),

    The ? operator

    The question mark operator is an abbreviation for the try! macro and already used in examples e.g. UdpSocket.

    is equivalent to:

    More information:
  11. Ranges and the .. operator

    More information.