Rust notes

Learning

TODO:

Wrapper types

Box This abstraction is a low cost abstraction for dynamic allocation. If you want to allocate some memory on the heap and safely pass a pointer to that memory around, this is ideal.

Rc is a reference counted pointer for multiple owning.

Source: Wrapper types

Articles

  • Some nice tipps & tricks from federicoterzi
  • Great google book for learning rust in 3 days (android) google

Error handling

Rusts result type is a great construct for creating error chains, something like:

#![allow(unused)]
fn main() {
fn read_config_file(path: &str)
    -> Result<String, ConfigFileError> {
    std::fs::read_to_string(path)
        .map_err(ConfigFileError::ReadError)
}

fn parse_config_file(file: String) 
    -> Result<i32, ConfigFileError> {
    file.parse()
        .map_err(ConfigFileError::ParseError)
}

fn work_with_config_file(path: &str) 
    -> Result<i32, ConfigFileError> {
    // No ? required at all - just directly return
    // the Result, since the types match!
    read_config_file(path).and_then(parse_config_file)
    // .and_then(something_else).and_then(even_more_things) ....
}
}

Source: naiveai, bruntsushi

Last change: 2023-12-19, commit: 950328d