Moon_Raven
Moon_Raven

Reputation: 309

Passing arrays by value in Rust

I have a Rust function defined as

fn foo(array: [u32; 100]) {
    // ...
}

When calling this function, how is the argument being passed? Is the whole array actually being "passed by value" (copied on the stack), since the array's type implements the Copy trait?

Upvotes: 1

Views: 1586

Answers (1)

orlp
orlp

Reputation: 117781

When calling this function, how is the argument being passed? Is the whole array actually being "passed by value" (copied on the stack), since the array's type implements the Copy trait?

I assume you mean [u32; 100]. And yes, that is passing by value.

If this is undesirable you can use a Box<[u32; 100]> or just a Vec<u32>. Or if you simply wish to read/mutate the array rather than take ownership you can take a slice (&[u32]) or mutable slice (&mut [u32]) respectively.

Upvotes: 4

Related Questions