C
C
CNNRNN2020-01-23 19:07:30
Rust
CNNRNN, 2020-01-23 19:07:30

Is memory allocated when passing slice?

Good day!
Let's say I have an array of bytes and I need to turn it into a string.
To do this, I run through the array to the desired byte, create a slice and pass it to another function.
As I understand it, the slice is just a reference to the elements of the array, which means that memory will not be allocated?
Is an array with references to the elements of another array equal to a slice?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Lesnikov, 2020-01-23
@CNNRNN

As I understand it, the slice is just a reference to the elements of the array, which means that memory will not be allocated?

If you take out the "boxed slice" (Box<[u8]>, see https://doc.rust-lang.org/reference/types/slice.html ), then regular slices (`&[u8]` and `&mut [u8]`) does not allocate any extra memory - just a pointer to the first element + usesize to store the number of elements.
`[&u8]`? No - at least because it will require space for storing a pointer to each element.
https://doc.rust-lang.org/std/str/fn.from_utf8.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question