Is a slice a copy or a reference?

I haven't found an answer until now. If I create a slice of a string or array, will the sliced data be copied/cloned or is a slice just a reference to the begin and the end of the slice to the original data?

I assume a slice is just some kind of reference because it is a data type, but I'm not sure

Yes, a slice is a "Fat Pointer" into the existing array/slice that points to the first element and includes the number of elements.

Depends, you can't have a slice all on its own [T]/str. If you have a borrow of a slice &[T]/&mut str then there is no copy, but if you as for a copy with functions like copy_from_slice then you will get a copy.

slices cannot be implicitly copied; as @KrishnaSannasi pointed out, there may be methods to copy / Clone them, but then it will be obvious that a copy is taking place.

In all other situations, such as &array[i .. j], this is just creating a (fat) reference to the elements in the array.

For a visual representation of (fat) references to a slice, you can check out this post (talking about str's .trim() method):


Shame on me. I had already read the chapter, but was probably too tired and didn't quite understand it. I have read the chapter about String Slices again and in the first sentence I read "Reference".

Thanks all for your help

I think it's worth pointing out that the term "slice" is in a somewhat ambiguous place right now, as it can be used to refer either to (what I call) a bare slice [T] or a reference to a slice &[T]. I hope over time the terminology will settle and by the time Rust is an ISO standard there will only be one correct usage, but that will take another decade or three.