0

In Rust, given a string and a start and end byte offset – or, since it is possible to view a string as an array of bytes, a byte array and a start and end offset – what's the most efficient/idiomatic way to make another string from that slice?

I've got as far as finding String::from_utf8 which will take an entire byte array; is there an alternative function that can take a slice of a byte array? Or, is there a function that can extract part of a byte array, suitable for feeding as input to the above function? Or some other way to achieve the same goal?

rwallace
  • 31,405
  • 40
  • 123
  • 242
  • 1
    If you have a string, you can slice it directly: `&some_string[start_byte..end_byte]`. See also [`std::ops::Index`](https://doc.rust-lang.org/stable/std/ops/trait.Index.html). – Shepmaster Dec 20 '21 at 16:19
  • *`String::from_utf8` which will take an entire byte array* — not quite. It takes a **`Vec`**. See also [What is the difference between storing a Vec vs a Slice?](https://stackoverflow.com/q/32571441/155423); [What is the difference between a slice and an array?](https://stackoverflow.com/q/30794235/155423) – Shepmaster Dec 20 '21 at 16:32

0 Answers0