Buffer[n..] vs buffer.get(n..) in memory usage context

I found that buffer[n..] returns [T] when buffer.get(n..) returns reference to slice &[T]

So the question is: does buffer[n..] make copy of the all bytes unlike get?
Will get method use less of memory or it's same methods inside, but with panic only?

I want to make reference to bytes of the file (for io buffer), without copying its [u8] content into the memory, so definitively stuck with this everything (in memory usage context)

Use &buffer[n..] to return a reference to a slice. No bytes are copied.

A slice without a reference ([T]) is not very useful unless you're creating a boxed slice.

1 Like

Thanks,
it's bit strange because it looks like I create buffer[n..] then append reference.

for example:
data: &buffer[n..] means data referenced to buffer[n..],
where buffer[n..] is a copy of some bytes in buffer.

when buffer.get(n..) returns the direct reference to bytes in buffer

They do the same thing.

In general when you index something (xxx[i..] or just xxx[i]) this does not necessarily copy the elements. It is called a place expression in Rust, and whether the elements are copied or not depends on what you do with it. If you simply reference it by prepending a &, then no elements are copied.

2 Likes

That's called a place expression. The & operator converts a place expression to a reference.

1 Like

Just when I start to think that I understand something, I realize that I understand nothing (c)

Thanks, guys!

1 Like