From 28eec20354ebe0592c9c327148de86222d0977ed Mon Sep 17 00:00:00 2001 From: ethan-000 Date: Sun, 20 Aug 2023 15:00:25 +0100 Subject: [PATCH] str as_bytes doc --- docs/language_concepts/00_data_types.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/docs/language_concepts/00_data_types.md b/docs/language_concepts/00_data_types.md index 9f6dd7f..7c1fbd9 100644 --- a/docs/language_concepts/00_data_types.md +++ b/docs/language_concepts/00_data_types.md @@ -152,6 +152,20 @@ fn main(message : pub str<11>, hex_as_string : str<4>) { } ``` +You can convert a `str` to a byte array by calling `as_bytes()` +or a vector by calling `as_bytes_vec()`. + +```rust +fn main() { + let message = "hello world"; + let message_bytes = message.as_bytes(); + let mut message_vec = message.as_bytes_vec(); + assert(message_bytes.len() == 11); + assert(message_bytes[0] == 104); + assert(message_bytes[0] == message_vec.get(0)); +} +``` + ## Compound Types A compound type groups together multiple values into one type. Elements within a compound type can