3 ms·
The example won't compile because nickname is just a reference to the first three bytes of name, but then name is cleared, so nickname would be a bad reference.
by tspiteri 7y ago
The example won't compile because nickname is just a reference to the first three bytes of name, but then name is cleared, so nickname would be a bad reference. That are two ways to go around this:
1. Print the nickname before clearing the name so that nickname is still valid when you print it (swap lines 4 and 5).
2. Make nickname a copy of the first three bytes of name, rather then a reference to the first three bytes of name.
let nickname = String::from(&name[..3]);