How to initialize struct fields which reference each other

Not only is there an issue with initialization, there could also be issues with destruction, if GameIterator implemented Drop: the compiler would have to know that it needs to destruct game_iter before game_window, otherwise game_window would have a reference to a destroyed GameWindowGLFW while running its drop() method. There’s no way to pass the lifetime … Read more

Is there any trait that specifies numeric functionality?

You can use num or num-traits crates and bound your generic function type with num::Float, num::Integer or whatever relevant trait: use num::Float; // 0.2.1 fn main() { let f1: f32 = 2.0; let f2: f64 = 3.0; let i1: i32 = 3; println!(“{:?}”, sqrt(f1)); println!(“{:?}”, sqrt(f2)); println!(“{:?}”, sqrt(i1)); // error } fn sqrt<T: Float>(input: T) … Read more

What is Vec?

It means “Rust compiler, infer what type goes into the Vec“. And it is indeed analogous to the unused variable in Python (and in Rust itself), in that it represents a placeholder for a type, like it can represent a placeholder for a variable name. You can find an explanation in The Rust Programming Language … Read more

How to make a compiled Regexp a global variable?

You can use the lazy_static macro like this: use lazy_static::lazy_static; // 1.3.0 use regex::Regex; // 1.1.5 lazy_static! { static ref RE: Regex = Regex::new(r”hello (\w+)!”).unwrap(); } fn main() { let text = “hello bob!\nhello sue!\nhello world!\n”; for cap in RE.captures_iter(text) { println!(“your name is: {}”, &cap[1]); } } If you are using the 2015 edition … Read more

How do I use a C library in a Rust library compiled to WebAssembly?

TL;DR: Jump to “New week, new adventures” in order to get “Hello from C and Rust!” The nice way would be creating a WASM library and passing it to the linker. rustc has an option for that (and there seem to be source-code directives too): rustc <yourcode.rs> –target wasm32-unknown-unknown –crate-type=cdylib -C link-arg=<library.wasm> The trick is … Read more

Can I call C or C++ functions from Rust code?

Rust doesn’t support this directly, C++ function symbol mangling is implementation defined, so it will need a lot of support from Rust to handle this feature. It’s not impossible but it’s probably not going to happen. However, Rust claims to support the C language. This is clearly more easy to support, as it “only” needs … Read more

How to call a method when a trait and struct use the same method name?

To specify which method to call, whether inherent or provided from a trait, you want to use the fully qualified syntax: Type::function(maybe_self, needed_arguments, more_arguments) Trait::function(maybe_self, needed_arguments, more_arguments) Your case doesn’t work because Vec doesn’t have a method called get! get is provided from the Deref implementation to [T]. The easiest fix is to call as_slice … Read more

Parameter type may not live long enough?

There are actually plenty of types that can “not live long enough”: all the ones that have a lifetime parameter. If I were to introduce this type: struct ShortLivedBee<‘a>; impl<‘a> Animal for ShortLivedBee<‘a> {} ShortLivedBee is not valid for any lifetime, but only the ones that are valid for ‘a as well. So in your … Read more