ํ—ˆ์ƒ(dangling) ์ฐธ์กฐ

์ด์ œ 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์€ ๋ฌธ์ž์—ด์„ ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ๋ฒ„ํผ์ž…๋‹ˆ๋‹ค.
This slide should take about 10 minutes.
  • &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 the push() and push_str() methods.

  • The format!() macro is a convenient way to generate an owned string from dynamic values. It accepts the same format specification as println!().

  • You can borrow &str slices from String via & and optionally range selection. If you select a byte range that is not aligned to character boundaries, the expression will panic. The chars iterator iterates over characters and is preferred over trying to get character boundaries right.

  • For C++ programmers: think of &str as std::string_view from C++, but the one that always points to a valid string in memory. Rust String is a rough equivalent of std::string from 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]);
    }