Should I use bytes or string?

It seems like bytes and string are pretty much the same, except that bytes is mutable and string is immutable. Does it mean that everywhere I use bytearrays, but are not interested in mutability, I should use string? This seems like a poor man mut keyword that’s missing from OCaml

1 Like

It depends what you’re doing with it. I use both. Some libraries use strings and some use bytes or both. If you’re careful about ownership you can use the unsafe conversions between the two to avoid copying things around when intefacing with different libraries.

2 Likes

I think this is a good question. It’s certainly not one that most people think about, but I do see it come up from time to time.

The answer depends on what you’re trying to do. If you’re working with data that is human-readable, then you probably want to use strings. However, if you’re working with binary data (such as images or audio), then bytes may be more appropriate.

3 Likes