๋ฌธ์ž์—ด

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.