How do I match based on a dynamic variable?
You can use Match guards. let string = match current % 2 { even if even == numbers[0] => “even”, odd if odd == numbers[1] => “odd”, _ => unreachable!() };
You can use Match guards. let string = match current % 2 { even if even == numbers[0] => “even”, odd if odd == numbers[1] => “odd”, _ => unreachable!() };
Rust generally solves errors in two ways: Unrecoverable errors. Once you panic!, that’s it. Your program or thread aborts because it encounters something it can’t solve and its invariants have been violated. E.g. if you find invalid sequences in what should be a UTF-8 string. Recoverable errors. Also called failures in some documentation. Instead of … Read more
You cannot do such a thing in stable Rust. Your example of 1000 * “abc” is not run at “compile time” in Python either, as far as I understand Python. Including a file If it has to be static, you could use a Cargo build script. This is a bit of Rust code that can … Read more
It seems like I could create a Hasher (e.g. RandomState), use that to manually calculate hash values, then store the u64 result in a HashMap<u64, _> but that seems kind of overly complex. Unfortunately that’s overly simple – since a hash function discards some information, hash tables don’t just work on hashes, they also need … Read more
what does it mean “generic type parameters that are filled in with concrete type parameters when the trait is used” An example that won’t work is when the type parameter is part of a method: trait Foo { fn foo<T>(t: T) {} } When a function has a type parameter, Rust will monomorphize the function … Read more
No, it is not freed immediately. Let’s make the code tell us itself: struct Foo(u8); impl Drop for Foo { fn drop(&mut self) { println!(“Dropping {}”, self.0) } } fn main() { let a = Foo(1); let b = Foo(2); println!(“All done!”); } The output is: All done! Dropping 2 Dropping 1 For me, this … Read more
Editor’s note: This specific problem has been solved by the introduction of non-lexical lifetimes. Let’s look at a tiny reproduction: struct Site { owner: u8, } struct GameMap { site: Site, } impl GameMap { fn do_anything(&self) {} fn get_site(&mut self) -> &mut Site { &mut self.site } } fn main() { let mut game_map … Read more
This can be done in several ways. Using slice::windows slice::windows: Returns an iterator over all contiguous windows of length size. In this case, the window size will be 2 so windows will return an iterator which will mask 2 elements and it will move to the right on every iteration. initial position : [|0, 5|, … Read more
Box::new only works with sized types; that is, it takes a value of a sized type T and returns Box<T>. In certain places a Box<T> can be coerced into a Box<U> (if T: Unsize<U>). Such coercion does not happen in .map(Box::new), but does in Some(Box::new(s)); the latter is basically the same as Some(Box::new(s) as Box<FooTrait>). … Read more
The simplest solution is to break the callback out from self, like so: let callback = self.click_callback.take(); if let Some(ref mut c) = callback { c(self); } self.click_callback = callback; self This temporarily replaces click_callback with None, hence why you have to put it back when you’re done.