{"id":8054,"date":"2024-10-25T23:24:35","date_gmt":"2024-10-25T14:24:35","guid":{"rendered":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=8054"},"modified":"2023-12-15T15:17:22","modified_gmt":"2023-12-15T06:17:22","slug":"rust-%eb%ac%b8%eb%b2%95-%ec%a0%95%eb%a6%ac","status":"publish","type":"post","link":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=8054","title":{"rendered":"Rust \u2013 \ubb38\ubc95 \uc815\ub9ac"},"content":{"rendered":"<h1>Rust \u2013 \ubb38\ubc95 \uc815\ub9ac<\/h1>\n<p>\uc544\ub798\uc758 \uc124\uba85\ub4e4\uc740 <a href=\"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=8042\">\uc5ec\uae30<\/a> \uc5d0 \uc124\uba85\ub41c \ubc29\uc2dd\uc73c\ub85c \uac1c\ubc1c\ud658\uacbd\uc774 \uad6c\ucd95\ub41c \uc0c1\ud669\uc744 \uc804\uc81c\ub85c \uc124\uba85\ud569\ub2c8\ub2e4.<\/p>\n<h2>\ubcc0\uc218<\/h2>\n<pre><code class=\"language-rust\">fn main() {\n    let x: i32 = 10;\n    let mut y: i32 = 20;\n    y = y + x;\n    println!(&quot;x: {x}&quot;);\n    println!(&quot;y: {y}&quot;);\n}<\/code><\/pre>\n<p>\ubcc0\uc218\ub294 \uae30\ubcf8\uc801\uc73c\ub85c \ubd88\ubcc0(immutable) \uc785\ub2c8\ub2e4.<br \/>\n\ubcc0\uacbd \uac00\ub2a5\ud558\uac8c \ud558\ub824\uba74 <code>mut<\/code> \ud0a4\uc6cc\ub4dc\ub97c \ubd99\uc5ec\uc8fc\uc5b4\uc57c \ud569\ub2c8\ub2e4.<\/p>\n<p>\ubcc0\uc218 \ud0c0\uc785\uc740 \uc0dd\ub7b5 \uac00\ub2a5\ud569\ub2c8\ub2e4.<br \/>\n\uc0dd\ub7b5\ub41c \ud0c0\uc785\uc740 \ucef4\ud30c\uc77c\ub7ec\uac00 \uc790\ub3d9\uc73c\ub85c \ud0c0\uc785\uc744 \ucd94\ub860\ud569\ub2c8\ub2e4.<\/p>\n<h3>\ud0c0\uc785 \ucd94\ub860<\/h3>\n<pre><code class=\"language-rust\">fn takes_u32(x: u32) {\n    println!(&quot;u32: {x}&quot;);\n}\n\nfn takes_i8(y: i8) {\n    println!(&quot;i8: {y}&quot;);\n}\n\nfn main() {\n    let x = 10;\n    let y = 20;\n\n    takes_u32(x);\n    takes_i8(y);\n}<\/code><\/pre>\n<p>\ubcc0\uc218\uc758 \ud0c0\uc785\uc740 \uc0dd\ub7b5\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<br \/>\n\ucef4\ud30c\uc77c\ub7ec\ub294 \ubcc0\uc218\uac00 \uc0ac\uc6a9\ub41c \uc0c1\ud0dc\ub97c \uae30\ubc18\uc73c\ub85c \ubcc0\uc218\uc758 \ud0c0\uc785\uc744 \uc790\ub3d9\ucd94\ub860\ud574\uc90d\ub2c8\ub2e4.<\/p>\n<h3>\uc0c1\uc218<\/h3>\n<pre><code class=\"language-rust\">const DIGEST_SIZE: usize = 3;\nstatic BANNER: &amp;str = &quot;Welcome to Rust&quot;;\n\nfn main() {\n    let _size = DIGEST_SIZE;\n    let _banner = BANNER;\n}<\/code><\/pre>\n<p><code>const<\/code>, <code>static<\/code> \ubaa8\ub450 \uc0c1\uc218\ub97c \ud45c\ud604\ud558\uc9c0\ub9cc \uc791\ub3d9\uc774 \ub2e4\ub985\ub2c8\ub2e4.<br \/>\n<code>const<\/code> \ub294 \ud568\uc218\uc5d0\uc11c \uc774\uc6a9\ub420 \ub54c \ud568\uc218\uc5d0 \ud560\ub2f9\ub41c \uc601\uc5ed\uc73c\ub85c \uac12\uc774 \ubcf5\uc0ac\ub429\ub2c8\ub2e4.<br \/>\n<code>static<\/code> \uc758 \uacbd\uc6b0\ub294 \ud504\ub85c\uadf8\ub7a8 \uc804\uc5ed\uc5d0 \ub2e8 \ud558\ub098\uc758 \uac12\ub9cc\uc774 \uc874\uc7ac\ud558\uace0, \ubaa8\ub4e0 \ud568\uc218\ub294 \uc774 \uac12\uc744 \uc774\uc6a9\ud569\ub2c8\ub2e4.<\/p>\n<p>\uba54\ubaa8\ub9ac \uc0ac\uc6a9\ub7c9 \uba74\uc5d0\uc11c\ub294 <code>static<\/code> \uc774 \uc720\ub9ac\ud558\uc9c0\ub9cc, \ud558\ub098\uc758 \ud568\uc218\uc5d0\uc11c \ubb38\uc790\uc5f4\uc758 \ub0b4\uc6a9\uc744 \uc218\uc815\ud560 \uacbd\uc6b0 \ubaa8\ub4e0 \ud568\uc218\uc5d0\uc11c \uc601\ud5a5\uc744 \ubc1b\uc73c\ubbc0\ub85c \uc704\ud5d8\uc131\ub3c4 \ub298\uc5b4\ub0a9\ub2c8\ub2e4.<\/p>\n<p>\uc77c\ubc18\uc801\uc73c\ub85c <code>const<\/code> \ub97c \uc0ac\uc6a9\ud558\ub294 \uac83\uc774 \uad8c\uc7a5\ub429\ub2c8\ub2e4.<\/p>\n<h3>\ubcc0\uc218 \uc228\uae30\uae30(Shadowing)<\/h3>\n<pre><code class=\"language-rust\">fn main() {\n    let a = 10;\n    println!(&quot;before: {a}&quot;);\n    {\n        let a = &quot;hello&quot;;\n        println!(&quot;inner scope: {a}&quot;);\n\n        let a = true;\n        println!(&quot;shadowed in inner scope: {a}&quot;);\n    }\n\n    println!(&quot;after: {a}&quot;);\n}<\/code><\/pre>\n<p>\ubc94\uc704 \ubc14\uae65 \ubfd0\ub9cc \uc544\ub2c8\ub77c, \ubc94\uc704 \ub0b4\ubd80\uc758 \ubcc0\uc218\ub3c4 \uc228\uae38 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<br \/>\n\ub3d9\uc77c \ubc94\uc704 \ub0b4\ubd80\uc5d0\uc11c\uc758 Shadowing \uc740 \uc0c8\ub85c\uc6b4 \ubcc0\uc218\ub97c \uc0dd\uc131\ud558\ub294 \uac83\uacfc \uac19\uc544\uc11c,<br \/>\n\ud0c0\uc785\uc744 \ubcc0\uacbd\ud560 \uc218\ub3c4 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n<h2>\uc5f4\uac70\ud615(enum)<\/h2>\n<pre><code class=\"language-rust\">use std::any::type_name;\nuse std::mem::{align_of, size_of};\n\n#[derive(Debug)]\nenum CoinFace {\n    Front, Back\n}\n\nfn main() {\n    println!(&quot;Coin face is {:?}&quot;, CoinFace::Front);\n    println!(&quot;Coin face is {:?}&quot;, CoinFace::Back);\n\n    println!(&quot;{}: size {} bytes, align: {} bytes&quot;,\n        type_name::&lt;CoinFace&gt;(), size_of::&lt;CoinFace&gt;(), align_of::&lt;CoinFace&gt;());\n}<\/code><\/pre>\n<p>enum \uc5d0 \uac12\uc744 \ud560\ub2f9\ud558\uc9c0 \uc54a\ub294 \uacbd\uc6b0 0, 1, 2, &#8230; \uc758 \uac12\uc774 \ud560\ub2f9\ub429\ub2c8\ub2e4.<\/p>\n<p>enum \uc758 \uc0ac\uc774\uc988\ub294 \ud544\uc694\ud55c \ucd5c\uc18c\ud55c\uc758 \uc0ac\uc774\uc988\ub97c \uac00\uc9d1\ub2c8\ub2e4.<br \/>\n(\ud558\uc9c0\ub9cc \uc774 \ubd80\ubd84\uc740 \uc124\uba85\uc774 \ubcf5\uc7a1\ud574\uc9c0\ubbc0\ub85c \uc9c1\uc811 \ud655\uc778\ud558\uae30 \ubc14\ub78d\ub2c8\ub2e4.)<\/p>\n<h2>\ud750\ub984 \uc81c\uc5b4<\/h2>\n<h3>\ube14\ub85d<\/h3>\n<pre><code class=\"language-rust\">fn main() {\n    let _x = {\n        3 + 4\n    };\n\n    let _y = {\n        let _ = 3 + 4;\n    };\n}<\/code><\/pre>\n<p>\ubaa8\ub4e0 \ube14\ub85d\uc740 \ub9ac\ud134 \uac12\uacfc \ud0c0\uc785\uc744 \uac00\uc9d1\ub2c8\ub2e4.<br \/>\n\ub9ac\ud134\ud558\ub294 \uac12\uc774 \uc5c6\uc73c\uba74 <code>()<\/code> \uc774 \ub9ac\ud134\uac12\uc774 \ub429\ub2c8\ub2e4.<br \/>\n\ub9ac\ud134\uac12\uc740 \uc138\ubbf8\ucf5c\ub860 <code>;<\/code> \uc774 \uc5c6\uc2b5\ub2c8\ub2e4.<\/p>\n<h2>if<\/h2>\n<pre><code class=\"language-rust\">fn main() {\n    let mut x = 10;\n    if x % 2 == 0 {\n        x = x \/ 2;\n    } else {\n        x = 3 * x + 1;\n    }\n}<\/code><\/pre>\n<p><code>else if<\/code> \uac00 \uc9c0\uc6d0\ub418\uc9c0\ub9cc <code>match<\/code> \ub97c \uc4f0\ub294 \uac83\uc774 \uad8c\uc7a5\ub429\ub2c8\ub2e4.<\/p>\n<pre><code class=\"language-rust\">fn main() {\n    let mut x = 10;\n    x = if x % 2 == 0 {\n        x \/ 2\n    } else {\n        3 * x + 1\n    };\n}<\/code><\/pre>\n<p>\ube14\ub85d\uc774 \ub9ac\ud134\uac12\uc774 \uc788\ub4ef\uc774, if \uc5ed\uc2dc \ub9ac\ud134\uac12\uc744 \uac00\uc9d1\ub2c8\ub2e4.<br \/>\n\ub9ac\ud134 \ud0c0\uc785\uc774 \ub3d9\uc77c\ud558\uc9c0 \uc54a\uc73c\uba74 \ucef4\ud30c\uc77c\ub7ec \uc624\ub958\uac00 \ubc1c\uc0dd\ud569\ub2c8\ub2e4.<\/p>\n<h3>for<\/h3>\n<pre><code class=\"language-rust\">fn main() {\n    let v = vec![10, 20, 30];\n\n    for x in v {\n        println!(&quot;x: {x}&quot;);\n    }\n\n    for i in (0..10).step_by(2) {\n        println!(&quot;i: {i}&quot;);\n    }\n}<\/code><\/pre>\n<p>\ub2e4\ub978 \uc5b8\uc5b4\uc640 \ub9c8\ucc2c\uac00\uc9c0\ub85c break \uc640 continue\ub97c \uc0ac\uc6a9\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n<h3>while<\/h3>\n<pre><code class=\"language-rust\">fn main() {\n    let mut x = 10;\n    while x != 1 {\n        x = if x % 2 == 0 {\n            x \/ 2\n        } else {\n            3 * x + 1\n        };\n    }\n    println!(&quot;Final x: {x}&quot;);\n}<\/code><\/pre>\n<h3>break, continue<\/h3>\n<pre><code class=\"language-rust\">fn main() {\n    let v = vec![10, 20, 30];\n    let mut iter = v.into_iter();\n    &#039;outer: while let Some(x) = iter.next() {\n        println!(&quot;x: {x}&quot;);\n        let mut i = 0;\n        while i &lt; x {\n            println!(&quot;x: {x}, i: {i}&quot;);\n            i += 1;\n            if i == 3 {\n                break &#039;outer;\n            }\n        }\n    }\n}<\/code><\/pre>\n<p>\uc911\ucca9 \ub8e8\ud504\uc5d0\uc11c \ub77c\ubca8\uc744 \uc774\uc6a9\ud574 \ube60\uc838 \ub098\uc62c \uc218 \uc788\uc2b5\ub2c8\ub2e4.<br \/>\nrust \ub294 <code>goto<\/code> \ub97c \uc9c0\uc6d0\ud558\uc9c0 \uc54a\uc2b5\ub2c8\ub2e4.<\/p>\n<h3>loop<\/h3>\n<pre><code class=\"language-rust\">fn main() {\n    let mut x = 10;\n    loop {\n        x = if x % 2 == 0 {\n            x \/ 2\n        } else {\n            3 * x + 1\n        };\n        if x == 1 {\n            break;\n        }\n    }\n    println!(&quot;Final x: {x}&quot;);\n}<\/code><\/pre>\n<h2>\ud2b9\uc218\ud55c \ud750\ub984 \uc81c\uc5b4<\/h2>\n<h3>if let<\/h3>\n<pre><code class=\"language-rust\">fn main() {\n    let arg = std::env::args().next();\n    if let Some(value) = arg {\n        println!(&quot;Program name: {value}&quot;);\n    } else {\n        println!(&quot;Missing name?&quot;);\n    }\n}<\/code><\/pre>\n<h3>while let<\/h3>\n<pre><code class=\"language-rust\">fn main() {\n    let v = vec![10, 20, 30];\n    let mut iter = v.into_iter();\n\n    while let Some(x) = iter.next() {\n        println!(&quot;x: {x}&quot;);\n    }\n}<\/code><\/pre>\n<h3>match<\/h3>\n<pre><code class=\"language-rust\">fn main() {\n    match std::env::args().next().as_deref() {\n        Some(&quot;cat&quot;) =&gt; println!(&quot;Will do cat things&quot;),\n        Some(&quot;ls&quot;)  =&gt; println!(&quot;Will ls some files&quot;),\n        Some(&quot;mv&quot;)  =&gt; println!(&quot;Let&#039;s move some files&quot;),\n        Some(&quot;rm&quot;)  =&gt; println!(&quot;Uh, dangerous!&quot;),\n        None        =&gt; println!(&quot;Hmm, no program name?&quot;),\n        _           =&gt; println!(&quot;Unknown program name!&quot;),\n    }\n}<\/code><\/pre>\n<p>\uc5ed\uc2dc \ube14\ub85d\uc774\ubbc0\ub85c \ub9ac\ud134\uac12\uc744 \uac00\uc9c8 \uc218 \uc788\uc9c0\ub9cc \ubaa8\ub4e0 \ub9ac\ud134\uac12\uc758 \ud0c0\uc785\uc740 \ub3d9\uc77c\ud574\uc57c \ud569\ub2c8\ub2e4.<\/p>\n<h2>\ud328\ud134 \ub9e4\uce6d<\/h2>\n<pre><code class=\"language-rust\">fn main() {\n    let input = &#039;x&#039;;\n\n    match input {\n        &#039;q&#039;                   =&gt; println!(&quot;Quitting&quot;),\n        &#039;a&#039; | &#039;s&#039; | &#039;w&#039; | &#039;d&#039; =&gt; println!(&quot;Moving around&quot;),\n        &#039;0&#039;..=&#039;9&#039;             =&gt; println!(&quot;Number input&quot;),\n        _                     =&gt; println!(&quot;Something else&quot;),\n    }\n}<\/code><\/pre>\n<p><code>_<\/code> \ub294 \uc704\uc758 \ub9e4\uce6d\uc774 \ubaa8\ub450 \uc2e4\ud328\ud588\uc744 \ub54c \ub9e4\uce6d\ub429\ub2c8\ub2e4.<\/p>\n<h3>\uc5f4\uac70\ud615(enum) \ub9e4\uce6d<\/h3>\n<pre><code class=\"language-rust\">enum Result {\n    Ok(i32),\n    Err(String),\n}\n\nfn divide_in_two(n: i32) -&gt; Result {\n    if n % 2 == 0 {\n        Result::Ok(n \/ 2)\n    } else {\n        Result::Err(format!(&quot;cannot divide {n} into two equal parts&quot;))\n    }\n}\n\nfn main() {\n    let n = 100;\n    match divide_in_two(n) {\n        Result::Ok(half) =&gt; println!(&quot;{n} divided in two is {half}&quot;),\n        Result::Err(msg) =&gt; println!(&quot;sorry, an error happened: {msg}&quot;),\n    }\n}<\/code><\/pre>\n<h3>\uad6c\uc870\uccb4(struct) \ub9e4\uce6d<\/h3>\n<pre><code class=\"language-rust\">struct Foo {\n    x: (u32, u32),\n    y: u32,\n}\n\n#[rustfmt::skip]\nfn main() {\n    let foo = Foo { x: (1, 2), y: 3 };\n    match foo {\n        Foo { x: (1, b), y } =&gt; println!(&quot;x = 1, b = {b}, y = {y}&quot;),\n        Foo { y: 2, x: i }   =&gt; println!(&quot;y = 2, x = {i:?}&quot;),\n        Foo { y, .. }        =&gt; println!(&quot;y = {y}, other fields were ignored&quot;),\n    }\n}<\/code><\/pre>\n<h3>\ubc30\uc5f4(array) \ub9e4\uce6d<\/h3>\n<pre><code class=\"language-rust\">#[rustfmt::skip]\nfn main() {\n    let triple = [0, -2, 3];\n    println!(&quot;Tell me about {triple:?}&quot;);\n    match triple {\n        [0, y, z] =&gt; println!(&quot;First is 0, y = {y}, and z = {z}&quot;),\n        [1, ..]   =&gt; println!(&quot;First is 1 and the rest were ignored&quot;),\n        _         =&gt; println!(&quot;All elements were ignored&quot;),\n    }\n}<\/code><\/pre>\n<p><code>[.., z]<\/code> \uac19\uc740 \ud615\uc2dd\uc73c\ub85c\ub3c4 \ub9e4\uce6d\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n<h3>\uc870\uac74\ubb38 \ub9e4\uce6d<\/h3>\n<pre><code class=\"language-rust\">#[rustfmt::skip]\nfn main() {\n    let pair = (2, -2);\n    println!(&quot;Tell me about {pair:?}&quot;);\n    match pair {\n        (x, y) if x == y     =&gt; println!(&quot;These are twins&quot;),\n        (x, y) if x + y == 0 =&gt; println!(&quot;Antimatter, kaboom!&quot;),\n        (x, _) if x % 2 == 1 =&gt; println!(&quot;The first one is odd&quot;),\n        _                    =&gt; println!(&quot;No correlation...&quot;),\n    }\n}<\/code><\/pre>\n<p><code>if<\/code> \uc870\uac74\ubb38\uc744 \uc774\uc6a9\ud574 \ub9e4\uce6d\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n<h2>\uba54\ubaa8\ub9ac \uad00\ub9ac<\/h2>\n<h3>\uc18c\uc720\uad8c<\/h3>\n<pre><code class=\"language-rust\">struct Point(i32, i32);\n\nfn main() {\n    {\n        let p = Point(3, 4);\n        println!(&quot;x: {}&quot;, p.0);\n    }\n    println!(&quot;y: {}&quot;, p.1);\n}<\/code><\/pre>\n<p>\ubaa8\ub4e0 \ubcc0\uc218\ub294 \ubc94\uc704\ub97c \uac00\uc9c0\uace0, \ubc94\uc704\ub97c \ubc97\uc5b4\ub098\uba74 \ubcc0\uc218\ub294 \uc0ac\ub77c\uc9d1\ub2c8\ub2e4.<br \/>\n\ubc94\uc704 \uc548\uc5d0\uc11c \ubcc0\uc218\ub294 \uac12\uc744 \uc18c\uc720(Ownership) \ud55c\ub2e4\uace0 \ud45c\ud604\ub2c8\ub2e4.<\/p>\n<h3>\uc18c\uc720\uad8c \uc774\ub3d9 : \ubcc0\uc218 \ud560\ub2f9<\/h3>\n<pre><code class=\"language-rust\">fn main() {\n    let s1: String = String::from(&quot;Hello!&quot;);\n    println!(&quot;s1: {s1}&quot;);\n\n    let s2: String = s1;\n    \/\/ println!(&quot;s1: {s1}&quot;);   \/\/ compiler error\n    println!(&quot;s2: {s2}&quot;);\n}<\/code><\/pre>\n<p>\uac12\uc744 \ub2e4\ub978 \ubcc0\uc218\uc5d0 \ud560\ub2f9\ud558\uba74 \uc18c\uc720\uad8c\uc744 \uc0c1\uc2e4\ud569\ub2c8\ub2e4.<br \/>\n<strong>\uc18c\uc720\uad8c\uc740 \ub2e8 \ud558\ub098\uc758 \ubcc0\uc218\ub9cc \uac00\uc9d1\ub2c8\ub2e4.<\/strong><br \/>\n\uc18c\uc720\uad8c\uc744 \uc0c1\uc2e4\ud558\uba74 \uac12\uc5d0 \ub300\ud55c \ubaa8\ub4e0 \uc811\uadfc\uc774 \ucc28\ub2e8\ub429\ub2c8\ub2e4.<\/p>\n<pre><code class=\"language-rust\">fn main() {\n    let s1: String = String::from(&quot;Hello!&quot;);\n    println!(&quot;s1: {s1}&quot;);\n\n    let s2: String = s1.clone();\n    println!(&quot;s1: {s1}&quot;);   \/\/ ok\n    println!(&quot;s2: {s2}&quot;);\n}<\/code><\/pre>\n<p>\ubcf5\uc0ac\ud560\ub54c\uc5d0\ub294 \uba85\uc2dc\uc801\uc73c\ub85c clone\uc744 \uc0ac\uc6a9\ud569\ub2c8\ub2e4.<\/p>\n<pre><code class=\"language-rust\">fn main() {\n    let i1 = 10;\n    println!(&quot;i: {i1}&quot;);\n\n    let i2 = i1;\n    println!(&quot;i1: {i1}&quot;);   \/\/ ok\n    println!(&quot;i2: {i2}&quot;);\n}<\/code><\/pre>\n<p>\uc815\uc218\uac19\uc740 \uac04\ub2e8\ud55c \uac12\uc740 \ubcf5\uc0ac\uac00 \ub429\ub2c8\ub2e4.<\/p>\n<h3>\uc18c\uc720\uad8c \uc774\ub3d9 : \ud568\uc218 \ud30c\ub77c\ubbf8\ud130 \ud560\ub2f9<\/h3>\n<pre><code class=\"language-rust\">fn say_hello(name: String) {\n    println!(&quot;Hello {name}&quot;)\n}\n\nfn main() {\n    let name = String::from(&quot;Alice&quot;);\n    say_hello(name);\n    \/\/ say_hello(name);     \/\/ compiler error\n}<\/code><\/pre>\n<p>\ud568\uc218 \ud30c\ub77c\ubbf8\ud130\ub85c \uc804\ub2ec\ud558\ub294 \uac83\ub9cc\uc73c\ub85c\ub3c4 \uc5ed\uc2dc \uc18c\uc720\uad8c\uc774 \uc774\ub3d9\ud569\ub2c8\ub2e4.<\/p>\n<pre><code class=\"language-rust\">fn say_hello(name: &amp;String) {\n    println!(&quot;Hello {name}&quot;)\n}\n\nfn main() {\n    let name = String::from(&quot;Alice&quot;);\n    say_hello(&amp;name);\n    say_hello(&amp;name);     \/\/ ok\n}<\/code><\/pre>\n<p>\ucc38\uc870\ub85c \uc804\ub2ec\ud558\uba74 \uc18c\uc720\uad8c \uc774\ub3d9\uc740 \ubc1c\uc0dd\ud558\uc9c0 \uc54a\uc2b5\ub2c8\ub2e4.<\/p>\n<h3>\ubcf5\uc0ac(copy)\uc640 \ubcf5\uc81c(clone)<\/h3>\n<pre><code class=\"language-rust\">fn main() {\n    let x = &quot;string&quot;;\n    let y = x;\n    println!(&quot;x: {x}&quot;);\n    println!(&quot;y: {y}&quot;);\n}<\/code><\/pre>\n<p>\uac1d\uccb4\uac00 \uc544\ub2c8\ub77c \ubb38\uc790\uc5f4 \ucc38\uc870\uac00 \ud560\ub2f9\ub418\ubbc0\ub85c \uc18c\uc720\uad8c \uc774\ub3d9\uc774 \uc5c6\uc2b5\ub2c8\ub2e4.<br \/>\n\uc815\uc218\uc640 \uac19\uc774 \uac04\ub2e8\ud55c \uac12\uc73c\ub85c \ucde8\uae09\ub418\uae30 \ub54c\ubb38\uc785\ub2c8\ub2e4.<\/p>\n<pre><code class=\"language-rust\">#[derive(Copy, Clone, Debug)]\nstruct Point(i32, i32);\n\nfn main() {\n    let p1 = Point(3, 4);\n    let p2 = p1;\n    println!(&quot;p1: {p1:?}&quot;);\n    println!(&quot;p2: {p2:?}&quot;);\n}<\/code><\/pre>\n<p><code>Copy<\/code> \ud2b8\ub808\uc787\uc744 \uad6c\ud604\ud558\uc5ec \ubcf5\uc0ac\ub97c \ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4:<br \/>\n\uba85\uc2dc\uc801\uc73c\ub85c <code>p1.clone()<\/code> \uc744 \uc0ac\uc6a9\ud558\uc5ec \ub370\uc774\ud130\ub97c \ubcf5\uc0ac\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n<blockquote>\n<p>\ubcf5\uc0ac\uc640 \ubcf5\uc81c\uc758 \ucc28\uc774<\/p>\n<p>\ubcf5\uc0ac(copy) \ub294 \uc544\ubb34\ub7f0 \ucee4\uc2a4\ud130\ub9c8\uc774\uc9d5 \uc5c6\uc774 \uc6d0\ubcf8 \uac12\uc744 \uadf8\ub300\ub85c \ubcf5\uc0ac\ud569\ub2c8\ub2e4.<br \/>\n\ubcf5\uc81c(clone) \ub294 \ucee4\uc2a4\ud130\ub9c8\uc774\uc9d5\uc774 \uac00\ub2a5\ud569\ub2c8\ub2e4.<br \/>\nString \uc740 \uac12 \uc790\uccb4\uac00 \uc544\ub2c8\ub77c \ubb38\uc790\uc5f4\uc5d0 \ub300\ud55c \uc8fc\uc18c\uac12\ub9cc \uac00\uc9c0\uace0 \uc788\uae30\uc5d0 copy \ub97c \uad6c\ud604\ud558\uc9c0 \uc54a\uc2b5\ub2c8\ub2e4.<br \/>\n\ub530\ub77c\uc11c \uad6c\uc870\uccb4\uc758 \ub9f4\ubc84 \uc911 \ud558\ub098\uac00 String \uc744 \ud3ec\ud568\ud558\uba74 \uadf8 \uad6c\uc870\uccb4\ub3c4 copy \ub97c \uad6c\ud604\ud560 \uc218 \uc5c6\uc2b5\ub2c8\ub2e4.<\/p>\n<\/blockquote>\n<h3>\ucc38\uc870 \uc81c\ud55c<\/h3>\n<p>\uacf5\uc720\uac00\ub2a5\ud55c \ubd88\ubcc0 \uacfc \uc720\uc77c\ud55c \uac00\ubcc0 \uc73c\ub85c \ud45c\ud604\ud569\ub2c8\ub2e4.<\/p>\n<p>\ud55c\ubc88\uc5d0 \ud558\ub098 \uc774\uc0c1\uc758 &amp;T (\uc218\uc815 \ubd88\uac00\ud55c \ucc38\uc870) \uac12\uc744 \uac00\uc9c0\uac70\ub098, \ub610\ub294<br \/>\n\uc815\ud655\ud788 \ud558\ub098\uc758 &amp;mut T (\uc218\uc815 \uac00\ub2a5\ud55c \ucc38\uc870) \uac12\ub9cc\uc744 \uac00\uc9c8 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n<h4>\uacf5\uc720\uac00\ub2a5\ud55c \ubd88\ubcc0<\/h4>\n<p>\ud558\ub098\uc758 \ubcc0\uc218\uc5d0 \ub300\ud574 \ub2e4\uc218\uc758 \ubd88\ubcc0 \ubcc0\uc218\ub294 \uac00\ub2a5\ud569\ub2c8\ub2e4.<\/p>\n<pre><code class=\"language-rust\">fn main() {\n    let a: i32 = 10;\n    let b: &amp;i32 = &amp;a;\n    let c: &amp;i32 = &amp;a;\n\n    println!(&quot;a: {a}&quot;);\n    println!(&quot;b: {b}&quot;);\n    println!(&quot;c: {c}&quot;);\n}<\/code><\/pre>\n<h4>\uc720\uc77c\ud55c \uac00\ubcc0<\/h4>\n<p>\uac00\ubcc0 \ucc38\uc870\ub294 \ub2e8 \ud558\ub098\ub9cc \uac00\ub2a5\ud569\ub2c8\ub2e4.<\/p>\n<pre><code class=\"language-rust\">fn main() {\n    let a: &amp;mut i32 = &amp;mut 10;\n    let b: &amp;mut i32 = a;\n    let c: &amp;mut i32 = a; \/\/ error[E0499]: cannot borrow `*a` as mutable more than once at a time\n\n    *b = 20;\n    *c = 30;\n\n    \/\/ println!(&quot;a: {a}&quot;);\n    println!(&quot;b: {b}&quot;);\n    println!(&quot;c: {c}&quot;);\n}<\/code><\/pre>\n<h2>\uad6c\uc870\uccb4(struct)<\/h2>\n<h3>\uad6c\uc870\uccb4 \uc77c\ubc18<\/h3>\n<pre><code class=\"language-rust\">struct Person {\n    name: String,\n    age: u8,\n}\n\nfn main() {\n    let mut peter = Person {\n        name: String::from(&quot;Peter&quot;),\n        age: 27,\n    };\n    println!(&quot;{} is {} years old&quot;, peter.name, peter.age);\n\n    peter.age = 28;\n    println!(&quot;{} is {} years old&quot;, peter.name, peter.age);\n\n    let jackie = Person {\n        name: String::from(&quot;Jackie&quot;),\n        ..peter\n    };\n    println!(&quot;{} is {} years old&quot;, jackie.name, jackie.age);\n}<\/code><\/pre>\n<p><code>..peter<\/code> \ubb38\ubc95\uc740 \ud55c \uad6c\uc870\uccb4\uc5d0\uc11c \ub2e4\ub978 \uad6c\uc870\uccb4\ub85c \ub300\ubd80\ubd84\uc758 \uac12\uc744 \ubcf5\uc0ac\ud558\ub824\uace0 \ud558\ub294 \uacbd\uc6b0\uc5d0 \ud558\ub098\ud558\ub098 \ud0c0\uc774\ud551\ud558\ub294 \uc218\uace0\ub97c \ub35c\uc5b4\uc90d\ub2c8\ub2e4.<br \/>\n\ubc18\ub4dc\uc2dc \ub9e8 \ub9c8\uc9c0\ub9c9\uc5d0 \uc640\uc57c \ud569\ub2c8\ub2e4.<\/p>\n<h3>\ud29c\ud50c \uad6c\uc870\uccb4<\/h3>\n<pre><code class=\"language-rust\">struct Point(i32, i32);\n\nfn main() {\n    let p = Point(17, 23);\n    println!(&quot;({}, {})&quot;, p.0, p.1);\n}<\/code><\/pre>\n<p>\ud544\ub4dc\uba85 \uc5c6\uc774 \uad6c\uc870\uccb4\ub97c \uc0dd\uc131\ud560 \uc218\ub3c4 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n<h3>\uad6c\uc870\uccb4 \uba54\uc18c\ub4dc<\/h3>\n<pre><code class=\"language-rust\">#[derive(Debug)]\nstruct Person {\n    name: String,\n    age: u8,\n}\n\nimpl Person {\n    fn new(name: String, age: u8) -&gt; Person {\n        Person { name, age }\n    }\n    fn say_hello(&amp;self) {\n        println!(&quot;Hello, my name is {}&quot;, self.name);\n    }\n}\n\nfn main() {\n    let peter = Person::new(String::from(&quot;Peter&quot;), 27);\n    println!(&quot;{peter:?}&quot;);\n    peter.say_hello();\n}<\/code><\/pre>\n<p><code>impl<\/code> \ud0a4\uc6cc\ub4dc\ub97c \uc774\uc6a9\ud574 \uba54\uc18c\ub4dc\ub97c \uc0dd\uc131\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n<pre><code class=\"language-rust\">impl Person {\n    fn new1(name: String, age: u8) -&gt; Self {\n        Self { name, age }\n    }\n}<\/code><\/pre>\n<p><code>Person<\/code> \uc740 <code>Self<\/code> \ub85c \ubcc0\uacbd\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n<p>\uba54\uc18c\ub4dc\uc758 \uccab\ubc88\uc9f8 \ud30c\ub77c\ubbf8\ud130\uac00 <code>&amp;self<\/code> \uc778\uac83\uc744 \ud655\uc778\ud558\uc138\uc694.<\/p>\n<h3>\uba54\uc11c\ub4dc \ub9ac\uc2dc\ubc84(Receiver)<\/h3>\n<p>\ud30c\ub77c\ubbf8\ud130\uc5d0 self \ub97c \ud560\ub2f9\ud558\ub294 \uac83\uc744 \ub9ac\uc2dc\ubc84\ub77c\uace0 \ud569\ub2c8\ub2e4.<\/p>\n<pre><code class=\"language-rust\">fn say_hello(&amp;self) { ... }\nfn say_hello(&amp;mut self) { ... }\nfn say_hello(self) { ... }\nfn say_hello(mut self) { ... }<\/code><\/pre>\n<p><code>&amp;self<\/code> : \ubd88\ubcc0 \ucc38\uc870\ub85c \ubc1b\uc2b5\ub2c8\ub2e4.<br \/>\n<code>&amp;mut self<\/code> : \uac12\uc744 \uc218\uc815\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<br \/>\n<code>self<\/code> : \uac1d\uccb4\uc758 \uc18c\uc720\uad8c\uc744 \uac00\uc838\uc635\ub2c8\ub2e4. \ub2e4\uc2dc \uac1d\uccb4\uc5d0 \uc18c\uc720\uad8c\uc744 \ubc18\ud658\ud558\uc9c0 \uc54a\uc73c\uba74 \uac1d\uccb4\ub294 \uac1d\uccb4\ub294 drop(\ud574\uc81c)\ub429\ub2c8\ub2e4.<br \/>\n<code>mut self<\/code> : \uac1d\uccb4\uc758 \uc18c\uc720\uad8c\uacfc \uc218\uc815\ud560 \uc218 \uc788\ub294 \uad8c\ud55c\uae4c\uc9c0 \ubc1b\uc2b5\ub2c8\ub2e4.<\/p>\n<h2>\ubaa8\ub4c8<\/h2>\n<pre><code class=\"language-rust\">mod foo {\n    pub fn do_something() {\n        println!(&quot;In the foo module&quot;);\n    }\n}\n\nmod bar {\n    pub fn do_something() {\n        println!(&quot;In the bar module&quot;);\n    }\n}\n\nfn main() {\n    foo::do_something();\n    bar::do_something();\n}<\/code><\/pre>\n<h3>\uac00\uc2dc\uc131<\/h3>\n<pre><code class=\"language-rust\">mod outer {\n    fn private() {\n        println!(&quot;outer::private&quot;);\n    }\n\n    pub fn public() {\n        println!(&quot;outer::public&quot;);\n        inner::public();\n    }\n\n    mod inner {\n        fn private() {\n            println!(&quot;outer::inner::private&quot;);\n        }\n\n        pub fn public() {\n            println!(&quot;outer::inner::public&quot;);\n            super::private();\n            private();\n        }\n    }\n}\n\nfn main() {\n    outer::public();\n}<\/code><\/pre>\n<p><code>pub<\/code> \ub97c \uc81c\uac70\ud558\uba74 \uc678\ubd80\uc5d0\uc11c \uc811\uadfc\uc774 \ubd88\uac00\ub2a5\ud569\ub2c8\ub2e4.<br \/>\n<code>pub<\/code> \ud0a4\uc6cc\ub4dc\ub294 \ubaa8\ub4c8\uc5d0\ub3c4 \uc0ac\uc6a9\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n<h3>\ud30c\uc77c \ubd84\ub9ac<\/h3>\n<p>\ud30c\uc77c\uc744 \ubd84\ub9ac\ud55c \uacbd\uc6b0 \ud30c\uc77c\uba85\uc774 \ubaa8\ub4c8\uba85\uc774 \ub429\ub2c8\ub2e4.<br \/>\n\ud30c\uc77c\uba85\uc740 \uae30\ubcf8 \ub77c\uc774\ube0c\ub7ec\ub9ac\uc758 \ub124\uc784\uc2a4\ud398\uc774\uc2a4\uc640 \uc911\ubcf5\uc774 \ub418\uc9c0 \uc54a\ub3c4\ub85d \ud574\uc57c \ud569\ub2c8\ub2e4.<\/p>\n<p>mylib.rs<\/p>\n<pre><code class=\"language-rust\">fn private() {\n    println!(&quot;outer::private&quot;);\n}\n\npub fn public() {\n    println!(&quot;outer::public&quot;);\n    inner::public();\n}\n\nmod inner {\n    fn private() {\n        println!(&quot;outer::inner::private&quot;);\n    }\n\n    pub fn public() {\n        println!(&quot;outer::inner::public&quot;);\n        super::private();\n        private();\n    }\n}<\/code><\/pre>\n<p>main.rs<\/p>\n<pre><code class=\"language-rust\">mod mylib;\n\nfn main() {\n    mylib::public();\n}<\/code><\/pre>\n<h2>\uc81c\ub108\ub9ad(generic)<\/h2>\n<pre><code class=\"language-rust\">#[derive(Debug)]\nstruct Point&lt;T&gt; {\n    x: T,\n    y: T,\n}\n\nfn main() {\n    let integer = Point { x: 5, y: 10 };\n    let float = Point { x: 1.0, y: 4.0 };\n    println!(&quot;{integer:?} and {float:?}&quot;);\n}<\/code><\/pre>\n<h3>\uc81c\ub108\ub9ad \uba54\uc11c\ub4dc<\/h3>\n<pre><code class=\"language-rust\">#[derive(Debug)]\nstruct Point&lt;T&gt;(T, T);\n\nimpl&lt;T&gt; Point&lt;T&gt; {\n    fn x(&amp;self) -&gt; &amp;T {\n        &amp;self.0  \/\/ + 10\n    }\n\n    \/\/ fn set_x(&amp;mut self, x: T)\n}\n\nfn main() {\n    let p = Point(5, 10);\n    println!(&quot;p.x = {}&quot;, p.x());\n}<\/code><\/pre>\n<h3>\ub2e8\ud615\ud654<\/h3>\n<p>\uc81c\ub108\ub9ad\uc740 \uc2e4\uc81c \ud638\ucd9c\uc774 \uc788\ub294 \uc2dc\uc810\uc5d0 \uc2e4\uc81c\ub85c \uc0dd\uc131\ub429\ub2c8\ub2e4.<br \/>\n\ubbf8\ub9ac \ub9cc\ub4e4\uc5b4 \ub193\uc740 \ud0c0\uc785\ub4e4\uc744 \uc0ac\uc6a9\ud558\ub294 \uac83\uc774 \uc544\ub2d9\ub2c8\ub2e4.<\/p>\n<h3>\ud2b8\ub808\uc787(Trait)<\/h3>\n<pre><code class=\"language-rust\">```\n\n```rust<\/code><\/pre>\n<pre><code class=\"language-rust\"><\/code><\/pre>\n<pre><code class=\"language-rust\"><\/code><\/pre>\n<pre><code class=\"language-rust\"><\/code><\/pre>\n<pre><code class=\"language-rust\"><\/code><\/pre>\n<pre><code class=\"language-rust\"><\/code><\/pre>\n<p><a href=\"https:\/\/google.github.io\/comprehensive-rust\/ko\/modules.html\">https:\/\/google.github.io\/comprehensive-rust\/ko\/modules.html<\/a><\/p>\n<h2>\uad6c\uc870\uccb4<\/h2>\n<h2>\uc870\uac74\ubb38<\/h2>\n<h2>\ubc18\ubcf5\uad6c\ubb38<\/h2>\n<h2>\ud568\uc218<\/h2>\n<h2>\uc81c\ub108\ub9ad(generic)<\/h2>\n<h2>\uc0c1\uc18d<\/h2>\n","protected":false},"excerpt":{"rendered":"<p>Rust \u2013 \ubb38\ubc95 \uc815\ub9ac \uc544\ub798\uc758 \uc124\uba85\ub4e4\uc740 \uc5ec\uae30 \uc5d0 \uc124\uba85\ub41c \ubc29\uc2dd\uc73c\ub85c \uac1c\ubc1c\ud658\uacbd\uc774 \uad6c\ucd95\ub41c \uc0c1\ud669\uc744 \uc804\uc81c\ub85c \uc124\uba85\ud569\ub2c8\ub2e4. \ubcc0\uc218 fn main() { let x: i32 = 10; let mut y: i32 = 20; y = y + x; println!(&quot;x: {x}&quot;); println!(&quot;y: {y}&quot;); } \ubcc0\uc218\ub294 \uae30\ubcf8\uc801\uc73c\ub85c \ubd88\ubcc0(immutable) \uc785\ub2c8\ub2e4. \ubcc0\uacbd \uac00\ub2a5\ud558\uac8c \ud558\ub824\uba74 mut \ud0a4\uc6cc\ub4dc\ub97c \ubd99\uc5ec\uc8fc\uc5b4\uc57c \ud569\ub2c8\ub2e4. \ubcc0\uc218 \ud0c0\uc785\uc740 \uc0dd\ub7b5 \uac00\ub2a5\ud569\ub2c8\ub2e4.\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=8054\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[],"class_list":["post-8054","post","type-post","status-publish","format-standard","hentry","category-rust-language"],"_links":{"self":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/8054","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8054"}],"version-history":[{"count":24,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/8054\/revisions"}],"predecessor-version":[{"id":8131,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/8054\/revisions\/8131"}],"wp:attachment":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8054"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8054"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8054"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}