λ¬Έμžμ—΄

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]);
}
This slide should take about 5 minutes.

이 μŠ¬λΌμ΄λ“œλŠ” λ¬Έμžμ—΄μ„ μ†Œκ°œν•©λ‹ˆλ‹€. 여기에 μžˆλŠ” λͺ¨λ“  λ‚΄μš©μ€ λ‚˜μ€‘μ— μžμ„Ένžˆ λ‹€λ£¨κ² μ§€λ§Œ 후속 μŠ¬λΌμ΄λ“œμ™€ μ—°μŠ΅λ¬Έμ œμ—μ„œ λ¬Έμžμ—΄μ„ μ‚¬μš©ν•˜λŠ” λ°λŠ” μ΄κ²ƒμœΌλ‘œ μΆ©λΆ„ν•©λ‹ˆλ‹€.

  • λ¬Έμžμ—΄ 내에 잘λͺ»λœ 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.