this post was submitted on 24 Dec 2024
33 points (100.0% liked)
Rust
6244 readers
16 users here now
Welcome to the Rust community! This is a place to discuss about the Rust programming language.
Wormhole
Credits
- The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)
founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
There is good reason to not do this. Though in this case it won't make much difference as performance is not an issue. But by having it outside the loop it allows the allocation of the string to be reused and in more complex programs is typically what you would want to do - allocate once outside the loop and reuse that inside.
In this case waiting on user input is going to outweigh any performance benefit I would not call it out as bad practice either.
Yes this is true, but it's a little advanced for this level so I didn't mention it. Probably better for beginners to focus on good code style than optimal performance.