- Mastering Rust
- Rahul Sharma Vesa Kaihlavirta
- 228字
- 2021-07-02 13:35:15
Strings
Strings are one of the most frequently used data types in any programming language. In Rust, they are usually found in two forms: the &str type (pronounced stir) and the String type. Rust strings are guaranteed to be valid UTF-8 encoded byte sequences. They are not null terminated as in C strings and can contain null bytes in-between them. The following program shows the two types in action:
// strings.rs
fn main() {
let question = "How are you ?"; // a &str type
let person: String = "Bob".to_string();
let namaste = String::from("??????"); // unicodes yay!
println!("{}! {} {}", namaste, question, person);
}
In the preceding code, person and namaste are of type String, while question is of type &str. There are multiple ways you can create String types. Strings are allocated on the heap, while &str types are usually pointers to an existing string, which could either be on stack, the heap, or a string in the data segment of the compiled object code. The & is an operator that is used to create a pointer to any type. After initializing the strings in the preceding code, we then use the println! macro to print them together using format strings. That's the very basics of strings. Strings are covered in detail in Chapter 7, Advanced Concepts.
- Arduino by Example
- Kali Linux Wireless Penetration Testing Beginner's Guide(Third Edition)
- Learning Network Forensics
- Java 11 Cookbook
- 快速念咒:MySQL入門指南與進(jìn)階實(shí)戰(zhàn)
- Arduino家居安全系統(tǒng)構(gòu)建實(shí)戰(zhàn)
- Access 2010數(shù)據(jù)庫(kù)應(yīng)用技術(shù)實(shí)驗(yàn)指導(dǎo)與習(xí)題選解(第2版)
- Java并發(fā)編程:核心方法與框架
- Advanced Python Programming
- Clojure Polymorphism
- Maven for Eclipse
- 從零開(kāi)始學(xué)UI設(shè)計(jì)·基礎(chǔ)篇
- 鋁合金陽(yáng)極氧化與表面處理技術(shù)(第三版)
- Java Web開(kāi)發(fā)任務(wù)教程
- SEO的藝術(shù)(原書第2版)