λ¬Έμμ΄
Rust has two types to represent strings, both of which will be covered in more depth later. Both always store UTF-8 encoded strings.
String
- a modifiable, owned string.&str
- μ½κΈ° μ μ© λ¬Έμμ΄μ λλ€. λ¬Έμμ΄ λ¦¬ν°λ΄μ μ΄ νμ μ κ°μ§λλ€.
fn main() { let greeting: &str = " μΈμ¬λ§"; let planet: &str = "πͺ"; let mut sentence = String::new(); sentence.push_str(greeting); sentence.push_str(", "); sentence.push_str(planet); println!("λ§μ§λ§ λ¬Έμ₯: {}", sentence); println!("{:?}", &sentence[0..5]); //println!("{:?}", &sentence[12..13]); }
μ΄ μ¬λΌμ΄λλ λ¬Έμμ΄μ μκ°ν©λλ€. μ¬κΈ°μ μλ λͺ¨λ λ΄μ©μ λμ€μ μμΈν λ€λ£¨κ² μ§λ§ νμ μ¬λΌμ΄λμ μ°μ΅λ¬Έμ μμ λ¬Έμμ΄μ μ¬μ©νλ λ°λ μ΄κ²μΌλ‘ μΆ©λΆν©λλ€.
-
λ¬Έμμ΄ λ΄μ μλͺ»λ UTF-8 μΈμ½λ©μ΄ μλ κ²μ μ μλμ§ μμ λμμ΄λ©°, μ΄λ μμ ν Rustμμλ νμ©λμ§ μμ΅λλ€.
-
String
μ μμ±μ(::new()
) λ°s.push_str(..)
κ³Ό κ°μ λ©μλκ° ν¬ν¨λ μ¬μ©μ μ μ νμ μ λλ€. -
&str
μ&
λ μ°Έμ‘°μμ λνλ λλ€. μ°Έμ‘°λ λμ€μ λ€λ£¨λ―λ‘ μ§κΈμ&str
μ βμ½κΈ° μ μ© λ¬Έμμ΄βμ μλ―Ένλ λ¨μλ‘ μκ°νμΈμ. -
μ£Όμ μ²λ¦¬λ μ€μ λ°μ΄νΈ μμΉλ³λ‘ λ¬Έμμ΄μ μμΈμ μμ±ν©λλ€.
12..13
μ λ¬Έμ κ²½κ³μμ λλμ§ μμΌλ―λ‘ νλ‘κ·Έλ¨μ΄ ν¨λ μνκ° λ©λλ€. μ€λ₯ λ©μμ§μ λ°λΌ λ¬Έμ κ²½κ³μμ λλλ λ²μλ‘ μ‘°μ ν©λλ€. -
Raw strings allow you to create a
&str
value with escapes disabled:r"\n" == "\\n"
. You can embed double-quotes by using an equal amount of#
on either side of the quotes:fn main() { println!(r#"<a href="link.html">link</a>"#); println!("<a href=\"link.html\">link</a>"); }
-
Using
{:?}
is a convenient way to print array/vector/struct of values for debugging purposes, and itβs commonly used in code.