- 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.
- Designing Machine Learning Systems with Python
- Android Studio Essentials
- MATLAB實用教程
- Learn React with TypeScript 3
- 精通Python自動化編程
- 零基礎入門學習Python(第2版)
- Mastering Web Application Development with AngularJS
- Scratch3.0趣味編程動手玩:比賽訓練營
- 零基礎學C語言程序設計
- IDA Pro權威指南(第2版)
- Java程序設計教程
- 從零開始學Python大數據與量化交易
- 從零開始學Unity游戲開發:場景+角色+腳本+交互+體驗+效果+發布
- HikariCP數據庫連接池實戰
- Learn Linux Quickly