Return local String as a slice (&str)

No, you cannot do it. There are at least two explanations why it is so. First, remember that references are borrowed, i.e. they point to some data but do not own it, it is owned by someone else. In this particular case the string, a slice to which you want to return, is owned by … Read more

How do I create a global, mutable singleton?

Non-answer answer Avoid global state in general. Instead, construct the object somewhere early (perhaps in main), then pass mutable references to that object into the places that need it. This will usually make your code easier to reason about and doesn’t require as much bending over backwards. Look hard at yourself in the mirror before … Read more

Why is it discouraged to accept a reference to a String (&String), Vec (&Vec), or Box (&Box) as a function argument?

TL;DR: One can instead use &str, &[T] or &T to allow for more generic code. One of the main reasons to use a String or a Vec is because they allow increasing or decreasing the capacity. However, when you accept an immutable reference, you cannot use any of those interesting methods on the Vec or … Read more

Traits and Rust

That block defines a new trait called Foo, which then allows you to use the trait in various places such as the impl block you have posted. The : Bar part says that any type that implements Foo must also implement the Bar trait.