How can I compare a slice with a byte string?

I have a method that received a &[u8] that represents a status code and I want to turn it into an enum, so I have a method that matches the slice to the enum. What I'm not sure is the best way to represent the values to match. Which one is cheaper: to convert the value to a String, represent with a byte string literal or as an inlined array?

pub(crate) fn from_slice(slice: &[u8]) -> Option<SqlStateCode> {
    match slice {
      b"3030303030" => Some(SqlStateCode::SuccessfulCompletion),
      b"3031303030" => Some(SqlStateCode::Warning),
      b"3031303043" => Some(SqlStateCode::DynamicResultSetsReturned),
      _=> None
    }
}

Byte string and inline array are exactly the same, both being cheaper than String (which needs to allocate)

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.