문자열 레퍼런스
이제 Rust의 두 가지 문자열 타입을 이해할 수 있습니다. &str은 거의 &[char]와 비슷하지만 데이터가 가변 길이 인코딩(UTF-8)으로 저장됩니다.
fn main() { let s1: &str = "World"; println!("s1: {s1}"); let mut s2: String = String::from("Hello "); println!("s2: {s2}"); s2.push_str(s1); println!("s2: {s2}"); let s3: &str = &s2[6..]; println!("s3: {s3}"); }
러스트 용어:
&str은 문자열 슬라이스에 대한 (불변) 참조입니다.String은 문자열을 담을 수 있는 버퍼입니다.
-
&str은 문자열 슬라이스입니다. 문자열 슬라이스는 UTF-8로 인코딩된 문자열 데이터를 의미합니다. 문자열 리터럴("Hello")은 프로그램 바이너리에 저장됩니다. -
러스트의
String타입은 실제로는 문자열을 이루는 바이트에 대한 백터(Vec<u8>)입니다.Vec<T>가T를 소유하고 있듯이,String이 가리키고 있는 문자열은String의 소유입니다. -
As with many other types
String::from()creates a string from a string literal;String::new()creates a new empty string, to which string data can be added using thepush()andpush_str()methods. -
The
format!()macro is a convenient way to generate an owned string from dynamic values. It accepts the same format specification asprintln!(). -
You can borrow
&strslices fromStringvia&and optionally range selection. If you select a byte range that is not aligned to character boundaries, the expression will panic. Thecharsiterator iterates over characters and is preferred over trying to get character boundaries right. -
For C++ programmers: think of
&strasstd::string_viewfrom C++, but the one that always points to a valid string in memory. RustStringis a rough equivalent ofstd::stringfrom C++ (main difference: it can only contain UTF-8 encoded bytes and will never use a small-string optimization). -
Byte strings literals allow you to create a
&[u8]value directly:fn main() { println!("{:?}", b"abc"); println!("{:?}", &[97, 98, 99]); }