String type
Doc.rust-lang.org/std/index.h… Don’t forget the RUST standard in your study
There are six main text types in Rust:
character
:The type of rust is charstring
:The type of rust is & STRraw string
:byte
byte string
raw byte string
String in the RUST standard library
char
To holdSingle Unicode character, occupying 4 bytes of space (32 bits).std::str
String slice type (string literal)std::string::String
The standard librarystd::char
use std::char;
println!("{}".char::from_u32(0x2764).unwrap()); / / ❤
Copy the code
The standard librarystd::str
STR is used with & to indicate the slice type. Basic data type, stored on a stack
let a: &str = "this is std::str"; // Slice type
let b = [12.23];
println!("{} - {:? }", a, b);
Copy the code
The STR slice type is not directly accessible (the dynamic size type) and must be used for the reference string. Below is a slice that generates another slice
let s: &str = "this slice type string"
let d: &str = &s[..5]
Copy the code
Sliced strings are immutable and can only be used by slicing reference parts in an applied manner. Of course, type conversions are covered later.
The standard librarystd::string::String
String is more flexible than the STR slice type. String can be used directly without using the use keyword. Reference data type, stored in the heap:
The new method creates a null character
let empty_str = String::new()
Copy the code
From creates a string
const str: String = String::from("this is flex string");
Copy the code
A conversion between STR and String
String -> STR is cheap
- Dereference: Use a Deref method for String
- use
+
A hyphen
STR -> String is not cheap
- redistribution