Do I need to deallocate memory manually in this case:
let mut s = String::new();
...somecode here...
s = String::new();
and is it the best way to erase content of the string?
Do I need to deallocate memory manually in this case:
let mut s = String::new();
...somecode here...
s = String::new();
and is it the best way to erase content of the string?
Copyright © 2021 Jogjafile Inc.
In such simple cases, Rust will automatically free memory when it us no longer needed.
If you want to assign a zero-length string to
s, you can use theclearfunction:This preserves the current capacity (and allocation) of the string. The alternative you cited,
does not do this. Both approaches have their uses, depending on the circumstances. Sometimes, retaining a large string allocation is wasteful (if the string will never grow to this size again).