Is there a faster/shorter way to initialize variables in a Rust struct?

You can provide default values for your struct by implementing the Default trait. The default function would look like your current new function: impl Default for cParams { fn default() -> cParams { cParams { iInsertMax: -1, iUpdateMax: -1, iDeleteMax: -1, iInstanceMax: -1, tFirstInstance: false, tCreateTables: false, tContinue: false, } } } You can then … Read more

What’s the difference between use and extern?

extern crate foo indicates that you want to link against an external library and brings the top-level crate name into scope (equivalent to use foo). As of Rust 2018, in most cases you won’t need to use extern crate anymore because Cargo informs the compiler about what crates are present. (There are one or two … Read more

How do I stop iteration and return an error when Iterator::map returns a Result::Err?

Result implements FromIterator, so you can move the Result outside and iterators will take care of the rest (including stopping iteration if an error is found). #[derive(Debug)] struct Item; type Id = String; fn find(id: &Id) -> Result<Item, String> { Err(format!(“Not found: {:?}”, id)) } fn main() { let s = |s: &str| s.to_string(); let … Read more

How to convert a String into a &’static str

Updated for Rust 1.0 You cannot obtain &’static str from a String because Strings may not live for the entire life of your program, and that’s what &’static lifetime means. You can only get a slice parameterized by String own lifetime from it. To go from a String to a slice &’a str you can … Read more

How to use a local unpublished crate?

Add a dependency section to your executable’s Cargo.toml and specify the path: [dependencies.my_lib] path = “../my_lib” or the equivalent alternate TOML: [dependencies] my_lib = { path = “../my_lib” } Check out the Cargo docs for specifying dependencies for more detail, like how to use a git repository instead of a local path.