How and what compiler thinks about this code?

Lifetime errors are sometimes a bit hard to understand. Even without understanding the errors fully, you should try to remember some antipatterns, in particular&'a mut Type where Type itself contains the same lifetime 'a syntactically is (almost) never something you’ll want to have, like the &'s mut &'s str you had here. Quite related, &'static mut Type is (almost) never something you’ll want.

Note that both cases talk about &'a mut reference, so mutable references. With immutable/shared references you might indeed sometimes encounter &'a Type where Type itself contains the same lifetime 'a, or &'static Type.

1 Like