Strings
Strings types are represented as UTF-8 encoded bytes.
| Data Type | Object | Slice | Vector | Option |
|---|---|---|---|---|
String referemce: &str | Yes | - | Yes | Yes |
String object: String | Yes | - | Yes | Yes |
Remarks:
- deserialization using
deserialize_fromwill validate if the string is a correct UTF-8 buffer. If you are certain that format is valid, you can usedeserialize_from_uncheckedto skip the validation step. This will speed up the deserialization process, but it is your responsibility to ensure that the string is actually a valid UTF-8 format. - You can use
Stringand&strinterchangeably, meaning that you can serialize an object that has aStringfield and deserialize it into a&str, or vice versa. This usually speeds up the deserialization process, as&stris a reference to a string slice and does not require allocation and copying of the data, whileStringis an owned collection of characters that requires allocation and copying of the data.
Example
-
Direct values:
#![allow(unused)] fn main() { use flat_message::*; #[derive(FlatMessage)] struct Example<'a> { string_value: String, str_value: &'a str, } } -
Vectors of strings or string slices:
#![allow(unused)] fn main() { use flat_message::*; #[derive(FlatMessage)] struct Example<'a> { string_values: Vec<String>, str_values: Vec<&'a str>, } } -
Using
Optionvalues:#![allow(unused)] fn main() { use flat_message::*; #[derive(FlatMessage)] struct Example<'a> { string_value: Option<String>, str_value: Option<&'a str>, } }