stacksonstacks
stacksonstacks

Reputation: 9313

What is the defacto bytes type in rust?

What is the defacto "bytes" type in rust? Say I have serialized some object what would be the expected type

In python there's bytes and in golang there's []byte. What is rust's equivalent?

Seems really simple but I guess I'm expressing this concept wrong, as I haven't found anything in searches

Upvotes: 6

Views: 7547

Answers (1)

loganfsmyth
loganfsmyth

Reputation: 161457

Often the best place to start is related Rust documentation. You mention reading data from a network connection, so let's look at TcpStream. It implements the Read trait's read method, which has the type

fn read(&mut self, buf: &mut [u8]) -> Result<usize>

which should make it relatively clear what you might expect. &mut [u8] is a mutable reference for a Rust slice where the underlying data is of type u8. Where specifically that u8 slice comes from is up to you as the caller. It could be from an array or Vec depending on your requirements for dynamic sizing, or any other type that supports it.

Upvotes: 10

Related Questions