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.

What is the correct way to return an Iterator (or any other trait)?

I’ve found it useful to let the compiler guide me: fn to_words(text: &str) { // Note no return type text.split(‘ ‘) } Compiling gives: error[E0308]: mismatched types –> src/lib.rs:5:5 | 5 | text.split(‘ ‘) | ^^^^^^^^^^^^^^^ expected (), found struct `std::str::Split` | = note: expected type `()` found type `std::str::Split<‘_, char>` help: try adding a … Read more

Is it possible to use global variables in Rust?

It’s possible, but heap allocation is not allowed directly. Heap allocation is performed at runtime. Here are a few examples: static SOME_INT: i32 = 5; static SOME_STR: &’static str = “A static string”; static SOME_STRUCT: MyStruct = MyStruct { number: 10, string: “Some string”, }; static mut db: Option<sqlite::Connection> = None; fn main() { println!(“{}”, … Read more

When does a closure implement Fn, FnMut and FnOnce?

The traits each represent more and more restrictive properties about closures/functions, indicated by the signatures of their call_… method, and particularly the type of self: FnOnce (self) are functions that can be called once FnMut (&mut self) are functions that can be called if they have &mut access to their environment Fn (&self) are functions … Read more