Mastering Rust by Rahul Sharma

Mastering Rust by Rahul Sharma

Author:Rahul Sharma
Language: eng
Format: epub
Publisher: Packt Publishing
Published: 2019-01-31T20:10:52+00:00


println!("Bytestring says {}", string_from_bytestring);

empty_string.push('1');

println!("1) Empty string now contains {}", empty_string);

empty_string.push_str("2345");

println!("2) Empty string now contains {}", empty_string);

println!("Length of the previously empty string is now {}",

empty_string.len());

}

With String explored, let's look at the borrowed version of strings known as string slices or the &str type.

Borrowed strings – &str

We can also have strings as references called string slices. These are denoted by &str (pronounced as stir), which is a reference to a str type. In constrast to the String type, str is a built-in type known to the compiler and is not something from the standard library. String slices are created as &str by default—a pointer to a UTF-8 encoded byte sequence. We cannot create and use values of the bare str type, as it represents a contiguous sequence of UTF-8 encoded bytes with a finite but unknown size. They are technically called unsized types. We'll explain unsized types later in this chapter.

str can only be created as a reference type. Let's assume we try to create a str type forcibly by providing the type signature on the left:

// str_type.rs



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.