Skip to content

Instantly share code, notes, and snippets.

@rrs42
Last active October 1, 2017 20:25
Show Gist options
  • Save rrs42/cb99b3002220a71341f7e96ca26e6617 to your computer and use it in GitHub Desktop.
Save rrs42/cb99b3002220a71341f7e96ca26e6617 to your computer and use it in GitHub Desktop.
Rust tricks
  • Include crate metadata in program
const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION");

// ...

println!("MyProgram v{}", VERSION.unwrap_or("unknown"));
  • Split Cargo.toml Authors into name,email pair (e.g. for use in App argument parser)
pub fn split_author_email(author: &str) -> Option<(String, String)> {

    let re = Regex::new(r"(.*) <(.*)>").unwrap();

    let parts = re.captures(author).unwrap();

    if parts.len() == 3 {
        return Some((
            String::from(parts.get(1).unwrap().as_str()),
            String::from(parts.get(2).unwrap().as_str()),
        ));
    }

    None
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment