let _ = .. is not a binding
When you are writing
1 | |
The return value is dropped immediately, because you cannot gave a value to _ as well as you cannot read it. _ is not a variable so this statement is not a variable binding.
When using RAII in rust, don’t forget this, use _{ident} to denote the return value. That thing is a variable so it follows correct scoping rules.
impl trait vs. dyn trait
We can write these two functions:
1 | |
impl ToString is static dispatch. The compiler specialize the function in compile-time, like a C++ template function.
dyn ToString is a dynamic dispatch, requiring a trait object which has a virtual function table. Here I use a reference because I didn’t require s binding by Size.