{"id":5620,"date":"2022-06-24T14:20:22","date_gmt":"2022-06-24T05:20:22","guid":{"rendered":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=5620"},"modified":"2022-06-24T16:47:15","modified_gmt":"2022-06-24T07:47:15","slug":"rust-%ec%9a%94%ec%95%bd","status":"publish","type":"post","link":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=5620","title":{"rendered":"Rust \uc694\uc57d(\uae30\ucd08)"},"content":{"rendered":"<h1>Rust \uc694\uc57d(\uae30\ucd08)<\/h1>\n<h2>\uc124\uce58\ud558\uae30<\/h2>\n<pre><code class=\"language-bash\">curl https:\/\/sh.rustup.rs -sSf | sh<\/code><\/pre>\n<h2>Hello, World!<\/h2>\n<p>\ud504\ub85c\uc81d\ud2b8 \uc0dd\uc131<\/p>\n<pre><code class=\"language-bash\">cargo new hello --bin\ncd hello\nls -al<\/code><\/pre>\n<p>\ube4c\ub4dc<\/p>\n<pre><code class=\"language-bash\">cargo build<\/code><\/pre>\n<p>\uc2e4\ud589<\/p>\n<pre><code class=\"language-bash\">.\/target\/debug\/hello<\/code><\/pre>\n<h2>\ubcc0\uc218\uc0dd\uc131<\/h2>\n<p><a href=\"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=4665\">\uc6d0\uaca9\uc5d0\uc11c ssh \uc5f0\uacb0<\/a> \uc744 \uc124\uc815\ud574 \ub450\uba74 \ud3b8\ud558\ub2e4.<\/p>\n<pre><code class=\"language-bash\">vi src\/main.rs<\/code><\/pre>\n<h3>\uac00\ubcc0\ud615 \/ \ubd88\ubcc0\ud615<\/h3>\n<p>rust \uae30\ubcf8\uc801\uc73c\ub85c \ubaa8\ub4e0 \ubcc0\uc218\ub294 \ubd88\ubcc0\ud615\uc774\ub2e4.(\ubcc0\uacbd \ubd88\uac00)<br \/>\n<code>mut<\/code> \ud0a4\uc6cc\ub4dc\ub97c \ubd99\uc5ec\uc11c \uac00\ubcc0\ud615 \ubcc0\uc218\ub85c \uc0dd\uc131\ud560 \uc218 \uc788\ub2e4.<br \/>\n\ub610\ub294 \uae30\uc874\uc5d0 \uc120\uc5b8\ud55c \ubcc0\uc218\ub97c \ub2e4\uc2dc \uc120\uc5b8\ud558\uba74\uc11c \uac12\uc744 \ubc14\uafb8\ub294 \uac83\uc740 \uac00\ub2a5\ud558\ub2e4.<\/p>\n<pre><code class=\"language-rust\">fn main() {\n    let a = 1;\n    a = a + 1;      \/\/ compile error\n\n    let mut b = 1;\n    b = b + 1;      \/\/ ok\n}<\/code><\/pre>\n<h3>\uc815\uc218\ud615<\/h3>\n<p>overflow \uc5d0 \uc0c1\ub2f9\ud788 \ubbfc\uac10\ud558\ub2e4.<br \/>\ncast \uc5f0\uc0b0\uc790\ub97c \ubd99\uc5ec \uc8fc\uc5b4\uc57c \uc624\ub958\uac00 \ubc1c\uc0dd\ud558\uc9c0 \uc54a\ub294\ub2e4.<\/p>\n<pre><code class=\"language-rust\">use std::any::type_name;\n\nfn type_of&lt;T&gt;(_: T) -&gt; &amp;&#039;static str {\n    type_name::&lt;T&gt;()\n}\n\nfn main() {\n    let a: i8 = -128;\n    let b: u8 = 255;\n\n    println!(&quot;Signed a == {}, Unsigned b == {}&quot;, a, b);\n\n    let a: i16 = 2;\n    let c: i32 = a.pow(14) as i32;\n\n    println!(&quot;c == {}&quot;, c);\n\n    let d = 100;\n\n    println!(&quot;type of d is {}&quot;, type_of(d));\n}<\/code><\/pre>\n<pre><code class=\"language-bash\">Signed a == -128, Unsigned b == 255\nc == 16384\ntype of d is i32<\/code><\/pre>\n<h3>\ubd80\ub3d9 \uc18c\uc218\uc810<\/h3>\n<p>\uc0c1\ud669\uc5d0 \ub530\ub77c \ud0c0\uc785\uc774 \ubcc0\ud55c\ub2e4.<\/p>\n<pre><code class=\"language-bash\">fn main() {\n    let x = 2.0;\n    println!(&quot;type of x is {}&quot;, type_of(x)); \/\/ f64\n}<\/code><\/pre>\n<pre><code class=\"language-bash\">fn main() {\n    let x = 2.0;\n    let y: f32 = 3.0;\n\n    let z = x * y;\n\n    println!(&quot;type of x is {}&quot;, type_of(x)); \/\/ f32\n    println!(&quot;type of z is {}&quot;, type_of(z)); \/\/ f32\n}<\/code><\/pre>\n<h3>Boolean\ud615<\/h3>\n<pre><code class=\"language-rust\">fn main() {\n    let a = true;\n    println!(&quot;type of a is {}&quot;, type_of(a));\n}<\/code><\/pre>\n<pre><code class=\"language-bash\">type of a is bool<\/code><\/pre>\n<h3>\ubb38\uc790\ud615<\/h3>\n<pre><code class=\"language-rust\">fn main() {\n    let c = &#039;A&#039;;\n    println!(&quot;type of c is {}&quot;, type_of(c));\n}<\/code><\/pre>\n<pre><code class=\"language-bash\">type of c is char<\/code><\/pre>\n<h3>\ubcf5\ud569\ud615(tuple)<\/h3>\n<pre><code class=\"language-bash\">fn main() {\n    let (a, b, c) = (1, 2, 3);\n    println!(&quot;a + b + c == {}&quot;, (a + b + c));\n}<\/code><\/pre>\n<h3>\ubc30\uc5f4(array)<\/h3>\n<pre><code class=\"language-rust\">fn main() {\n    let a = [1, 2, 3, 4, 5];\n    println!(&quot;a[1] == {}&quot;, a[1]);\n}<\/code><\/pre>\n<h2>\uc81c\uc5b4\ubb38<\/h2>\n<p>\uad04\ud638\ub97c \uc4f0\uba74 \ucef4\ud30c\uc77c\ub7ec \uacbd\uace0\uac00 \ub72c\ub2e4.<\/p>\n<h3>if else<\/h3>\n<pre><code class=\"language-rust\">fn main() {\n    let a = 1;\n    if a &gt; 1 {\n        println!(&quot;over&quot;);\n    } else if a &lt; 1 {\n        println!(&quot;below&quot;);\n    } else {\n        println!(&quot;equal&quot;);\n    }\n}<\/code><\/pre>\n<h3>let if<\/h3>\n<pre><code class=\"language-bash\">fn main() {\n    let a = true;\n    let b = if a { 1 } else { 2 };\n\n    println!(&quot;b == {}&quot;, b);\n}<\/code><\/pre>\n<h3>let with block<\/h3>\n<p>\ube14\ub85d\uc73c\ub85c \uac12\uc744 \uc9c0\uc815\ud560 \uc218 \uc788\ub2e4.<\/p>\n<pre><code class=\"language-bash\">fn main() {\n    let a = true;\n    let b = {\n        let a = 3;\n        a + 1\n    };\n\n    println!(&quot;b == {}&quot;, b);\n}\n<\/code><\/pre>\n<h3>switch case<\/h3>\n<p>\uc5c6\ub098??<\/p>\n<h2>\ubc18\ubcf5\uad6c\ubb38<\/h2>\n<h3>for<\/h3>\n<pre><code class=\"language-rust\">fn main() {\n    let a = [10, 20, 30, 40, 50];\n\n    for element in a.iter() {\n        println!(&quot;{}&quot;, element);\n    }\n\n    println!(&quot;-----------&quot;);\n\n    for number in (1..4).rev() {    \/\/ 4 \ub294 \uc81c\uc678\n        println!(&quot;{}&quot;, number);\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-bash\">10\n20\n30\n40\n50\n-----------\n3\n2\n1<\/code><\/pre>\n<h3>while<\/h3>\n<pre><code class=\"language-rust\">fn main() {\n    let mut num = 10;\n\n    while num != 0 {\n        println!(&quot;{}&quot;, num);\n        num = num - 1;\n    }\n}<\/code><\/pre>\n<h3>loop<\/h3>\n<pre><code class=\"language-rust\">fn main() {\n    loop {\n        println!(&quot;forever&quot;);\n    }\n}<\/code><\/pre>\n<h2>\ud568\uc218(function)<\/h2>\n<pre><code class=\"language-rust\">fn main() {\n    function_without_parameter();\n\n    function_with_parameter(100, 200);\n\n    let a = function_with_return_value();\n    println!(&quot;a == {}&quot;, a);\n}\n\nfn function_without_parameter() {\n    println!(&quot;function without parameter&quot;);\n}\n\nfn function_with_parameter(x: i32, y: i32) {\n    println!(&quot;function with parameter x == {}, y == {}&quot;, x, y);\n}\n\nfn function_with_return_value() -&gt; i32 {\n    5       \/\/ \uc138\ubbf8\ucf5c\ub860\uc744 \ubd99\uc774\uba74 \uc624\ub958\uac00 \ubc1c\uc0dd\ud55c\ub2e4.\n}<\/code><\/pre>\n<h2>\uad6c\uc870\uccb4(struct)<\/h2>\n<pre><code class=\"language-bash\">fn main() {\n    let user1 = User {\n        email: String::from(&quot;someone@example.com&quot;),\n        username: String::from(&quot;someusername123&quot;),\n        \/\/ active: true,\n        \/\/ sign_in_count: 1,\n    };\n\n    println!(&quot;name of user1 is {}&quot;, user1.username);\n    println!(&quot;email of user1 is {}&quot;, user1.email);\n}\n\nstruct User {\n    username: String,\n    email: String,          \/\/ \ub9c8\uc9c0\ub9c9 \ucf64\ub9c8\ub294 optional \uc774\ub2e4.\n    \/\/ sign_in_count: u64,\n    \/\/ active: bool,\n}<\/code><\/pre>\n<p>tuple struct<\/p>\n<pre><code class=\"language-rust\">fn main() {\n    let black = Color(0, 0, 0);\n    let origin = Point(10, 0);\n\n    println!(&quot;R value of black is {}&quot;, black.0);\n    println!(&quot;x value of origin is {}&quot;, origin.0);\n}\n\n\/\/ tuple struct\nstruct Color(i32, i32, i32);\nstruct Point(i32, i32);<\/code><\/pre>\n<h2>\uc5f4\uac70\ud615(enum)<\/h2>\n<pre><code class=\"language-rust\">fn main() {\n    let id: String = &quot;id&quot;.to_string();\n    let password: String = &quot;abcd1234&quot;.to_string();\n\n    if check_login(id, password) == ErrorCode::Ok {\n        println!(&quot;login ok&quot;);\n    }\n}\n\n#[derive(PartialEq)]\nenum ErrorCode {\n    Ok,\n    InvalidId,\n    InvalidPassword,\n}\n\nfn check_login(id: String, password: String) -&gt; ErrorCode {\n    if id != &quot;id&quot; {\n        ErrorCode::InvalidId\n    } else if password != &quot;abcd1234&quot; {\n        ErrorCode::InvalidPassword\n    } else {\n        ErrorCode::Ok\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Rust \uc694\uc57d(\uae30\ucd08) \uc124\uce58\ud558\uae30 curl https:\/\/sh.rustup.rs -sSf | sh Hello, World! \ud504\ub85c\uc81d\ud2b8 \uc0dd\uc131 cargo new hello &#8211;bin cd hello ls -al \ube4c\ub4dc cargo build \uc2e4\ud589 .\/target\/debug\/hello \ubcc0\uc218\uc0dd\uc131 \uc6d0\uaca9\uc5d0\uc11c ssh \uc5f0\uacb0 \uc744 \uc124\uc815\ud574 \ub450\uba74 \ud3b8\ud558\ub2e4. vi src\/main.rs \uac00\ubcc0\ud615 \/ \ubd88\ubcc0\ud615 rust \uae30\ubcf8\uc801\uc73c\ub85c \ubaa8\ub4e0 \ubcc0\uc218\ub294 \ubd88\ubcc0\ud615\uc774\ub2e4.(\ubcc0\uacbd \ubd88\uac00) mut \ud0a4\uc6cc\ub4dc\ub97c \ubd99\uc5ec\uc11c \uac00\ubcc0\ud615 \ubcc0\uc218\ub85c \uc0dd\uc131\ud560 \uc218 \uc788\ub2e4. \ub610\ub294 \uae30\uc874\uc5d0 \uc120\uc5b8\ud55c\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=5620\">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-5620","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\/5620","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=5620"}],"version-history":[{"count":15,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/5620\/revisions"}],"predecessor-version":[{"id":5635,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/5620\/revisions\/5635"}],"wp:attachment":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5620"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5620"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5620"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}