>>107643894
>Compare the rust code you don't really know what is created/destroyed
It allocates at these function calls only:
>String::from()
>to_string()
>collect()
The point of every one of these functions is to convert to an owned type, so most Rust programmers hopefully understand that they allocate.
Rust doesn't typically do any heap allocation without an explicit function call. C++ does it all the time thanks to its constructors.
All of them are actually superfluous AI slop. You can leave them out for a more concise version that doesn't allocate:
fn main() {
let text = "python rules sepples drools";
let words = text.split_whitespace();
for word in words {
println!("{}", word);
}
}
or just:
fn main() {
let text = "python rules sepples drools";
for word in text.split_whitespace() {
println!("{}", word);
}
}
This would be the C++ equivalent (though a bit less smart about whitespace):
#include <print>
#include <string_view>
#include <ranges>
int main () {
std::string_view text = "python rules sepples drools";
for (auto word : text | std::views::split(' ')) {
std::println("{}", std::string_view(word));
}
}