String and str

Doosan published on
1 min, 141 words

Categories: Post

Stringstr의 차이

StringVec와 비슷하게 heap에 저장되는, growable한 문자열 타입이다. C++의 std::string과 비슷하다고 볼 수 있다. str은 문자열 데이터의 slice 타입이다. 크기를 compile time에 알 수 없는 unsized type이므로 보통 단독으로 쓰지 않고 &str처럼 reference로 사용한다.

타입변환

String 타입을 &str으로 변환하려면? String 앞에 &*를 붙일 수 있다. Deref 참고.

let s = String::from("hello world")
let ss = *s; //str type, Deref coerce
let sss = &*s; //&str

또는 as_str() 함수를 사용한다.

let s = String::from("hello world")
let ss = s.as_str; //&str

&'static str 타입(string literal)을 String으로 변환하려면? to_string() 함수를 사용하면 된다. &strString으로 변환하면 새 메모리 할당이 필요하므로, 꼭 소유한 문자열이 필요한 경우에만 하자.

let s = "hello world";
let ss = s.to_string();

Byte String Literal

b"whatever"은 byte string literal이다. 타입은 &[u8; 8]처럼 byte array reference가 된다.

let s = b"hello world"; //buf:&[u8;11]
let ss =  b"whatever"; //buf:&[u8;8]