@diegovsky@fosstodon.org avatar

diegovsky

@diegovsky@fosstodon.org

Rust enthusiast and FOSS lover. CS Student @ UFSJ

This profile is from a federated server and may be incomplete. For a complete list of posts, browse on the original instance.

became_fish , to random
@became_fish@jorts.horse avatar

people say "C++ isn't any closer to the metal than is" but what is the memory footprint of returning a Result? in C++ i can pass an out parameter by reference to avoid allocating anything on the stack - how do i do that in rust? is it with a &mut ptr? is it done automatically by the compiler when returning?

diegovsky ,
@diegovsky@fosstodon.org avatar

@became_fish @migratory you don't need to convert into a String, you can just use .chars() if you have a &str

diegovsky ,
@diegovsky@fosstodon.org avatar

@became_fish a result is a very lightweight type tbh. Its stack size is very negligible and you wouldn't need to do that. The compiler is smart enough to optimise that, so it likely compiles down to a single register, or it inlines the function.

If you insist on going that route, though, yes, &mut is the way.

diegovsky ,
@diegovsky@fosstodon.org avatar

@became_fish @migratory if you don't want to allocate extra memory but still wants to iterate char-by-char, you can also use the .chars() method and consume one element at a time.

The returned value is a rust iterator that yields a char every time you call .next() on it. It doesn't allocate anything on the heap because it's actually just a pointer to the string + a counter

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • test
  • worldmews
  • mews
  • All magazines