References preferable to pass to function?

⚓ Rust    📅 2025-04-28    👤 surdeus    👁️ 3      

surdeus

Warning

This post was published 115 days ago. The information described in this article may have changed.

Hi all,

I'd like to decide how to pass some values to a function. This function's signature looks like

fn foo(&self, data: &[String]) -> u64

The function is implemented by some struct that does own the underlying Strings stored in a Vec<String>. The passed data is just some slice and I'd like to figure out how to pass that data to the function foo regarding performance.

My understanding of what is happening behind the scene when foo gets invoked says: Calling foo puts a reference to an array of Strings on the stack but leaves the Strings themself untouched (uncloned) on the heap. Regarding the costs a call to foo creates a single pointer on the stack but does nothing else and that would be best way to go.

Am I right?

3 posts - 2 participants

Read full topic

🏷️ rust_feed