{"id":1600,"date":"2024-09-09T20:33:48","date_gmt":"2024-09-09T12:33:48","guid":{"rendered":"https:\/\/lyvba.com\/?p=1600"},"modified":"2024-09-09T20:33:48","modified_gmt":"2024-09-09T12:33:48","slug":"rust-hello-world","status":"publish","type":"post","link":"https:\/\/lyvba.com\/index.php\/2024\/09\/09\/rust-hello-world\/","title":{"rendered":"\u7b80\u5355\u7684Rust\u7f16\u7a0b\u8bed\u8a00\u521d\u7ea7\u6559\u7a0b"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2024\/09\/Rust.webp\" alt=\"\" \/><\/p>\n<h3>Rust \u7b80\u4ecb<\/h3>\n<p>Rust \u662f\u4e00\u95e8\u4e13\u6ce8\u4e8e\u5b89\u5168\u6027\u548c\u6027\u80fd\u7684\u7cfb\u7edf\u7f16\u7a0b\u8bed\u8a00\u3002\u5b83\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u7f16\u5199\u9ad8\u6548\u3001\u5b89\u5168\u7684\u4ee3\u7801\u3002<\/p>\n<h3>\u5b89\u88c5 Rust<\/h3>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5 Rust\uff1a<\/strong> \u53ef\u4ee5\u901a\u8fc7 Rust \u5b98\u65b9\u7f51\u7ad9\u7684 <a href=\"https:\/\/rustup.rs\/\">Rustup<\/a> \u5b89\u88c5\u5de5\u5177\u5b89\u88c5 Rust\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u68c0\u67e5\u5b89\u88c5\uff1a<\/strong> \u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u9a8c\u8bc1\u5b89\u88c5\u662f\u5426\u6210\u529f\uff1a<\/p>\n<pre><code class=\"language-bash\">rustc --version<\/code><\/pre>\n<\/li>\n<\/ol>\n<h3>\u57fa\u672c\u8bed\u6cd5<\/h3>\n<h4>Hello, World!<\/h4>\n<p>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a <code>main.rs<\/code> \u7684\u6587\u4ef6\uff0c\u5e76\u8f93\u5165\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-rust\">fn main() {\n    println!(&quot;Hello, world!&quot;);\n}<\/code><\/pre>\n<p>\u8fd0\u884c\u7a0b\u5e8f\uff1a<\/p>\n<pre><code class=\"language-bash\">rustc main.rs\n.\/main<\/code><\/pre>\n<h4>\u53d8\u91cf\u548c\u53ef\u53d8\u6027<\/h4>\n<pre><code class=\"language-rust\">fn main() {\n    let x = 5; \/\/ \u4e0d\u53ef\u53d8\u53d8\u91cf\n    let mut y = 10; \/\/ \u53ef\u53d8\u53d8\u91cf\n    y = 15;\n    println!(&quot;x: {}, y: {}&quot;, x, y);\n}<\/code><\/pre>\n<h4>\u6570\u636e\u7c7b\u578b<\/h4>\n<p>Rust \u662f\u9759\u6001\u7c7b\u578b\u8bed\u8a00\uff0c\u5e38\u89c1\u7684\u6570\u636e\u7c7b\u578b\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u6574\u6570\uff1a<code>i8<\/code>, <code>u8<\/code>, <code>i32<\/code>, <code>u32<\/code>, \u7b49<\/li>\n<li>\u6d6e\u70b9\u6570\uff1a<code>f32<\/code>, <code>f64<\/code><\/li>\n<li>\u5e03\u5c14\uff1a<code>bool<\/code><\/li>\n<li>\u5b57\u7b26\uff1a<code>char<\/code><\/li>\n<li>\u5143\u7ec4\u548c\u6570\u7ec4<\/li>\n<\/ul>\n<pre><code class=\"language-rust\">fn main() {\n    let a: i32 = 10;\n    let b: bool = true;\n    let c: char = &#039;z&#039;;\n    let d: (i32, f64, u8) = (500, 6.4, 1);\n    let e: [i32; 3] = [1, 2, 3];\n}<\/code><\/pre>\n<h4>\u51fd\u6570<\/h4>\n<pre><code class=\"language-rust\">fn main() {\n    let result = add(5, 10);\n    println!(&quot;Result: {}&quot;, result);\n}\n\nfn add(x: i32, y: i32) -&gt; i32 {\n    x + y\n}<\/code><\/pre>\n<h4>\u63a7\u5236\u6d41<\/h4>\n<pre><code class=\"language-rust\">fn main() {\n    let number = 6;\n\n    if number % 4 == 0 {\n        println!(&quot;number is divisible by 4&quot;);\n    } else if number % 3 == 0 {\n        println!(&quot;number is divisible by 3&quot;);\n    } else {\n        println!(&quot;number is not divisible by 4 or 3&quot;);\n    }\n\n    \/\/ \u5faa\u73af\n    for i in 1..5 {\n        println!(&quot;{}&quot;, i);\n    }\n}<\/code><\/pre>\n<h3>\u7ec3\u4e60<\/h3>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\uff0c\u8ba1\u7b97\u4e24\u4e2a\u6570\u7684\u4e58\u79ef\u3002\n<pre><code class=\"language-rust\">\nfn mul(x: i32, y: i32) -&gt; i32 {return x * y;}\nfn main() {\nprintln!(&quot;Result: {}&quot;, mul(9, 9));\n}<\/code><\/pre>\n<\/li>\n<\/ol>\n<pre><code>2. \u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u6253\u5370\u4ece 1 \u5230 100 \u7684\u6240\u6709\u5076\u6570\u3002\n```rust\nfn main() {\n    for i in 1..=100 {\n        if i % 2 == 0 {\n            println!(&quot;{}&quot;, i);\n        }\n    }\n}<\/code><\/pre>\n<h3>\u603b\u7ed3<\/h3>\n<p>\u5728 Rust \u4e2d\uff0c\u51fd\u6570\u7684\u6700\u540e\u4e00\u4e2a\u8868\u8fbe\u5f0f\u7684\u8fd4\u56de\u503c\u53ef\u4ee5\u7701\u7565 <code>return<\/code> \u5173\u952e\u5b57\u3002\u6700\u540e\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4f1a\u88ab\u81ea\u52a8\u4f5c\u4e3a\u8fd4\u56de\u503c\u3002<\/p>\n<pre><code class=\"language-rust\">fn add(x: i32, y: i32) -&gt; i32 {\n    x + y \/\/ \u8fd9\u91cc\u7701\u7565\u4e86 return\n}<\/code><\/pre>\n<p>\u5982\u679c\u4f7f\u7528 <code>return<\/code>\uff0c\u5219\u9700\u8981\u52a0\u4e0a\u5206\u53f7\uff1a<\/p>\n<pre><code class=\"language-rust\">fn add(x: i32, y: i32) -&gt; i32 {\n    return x + y; \/\/ \u4f7f\u7528 return \u65f6\u9700\u8981\u5206\u53f7\n}<\/code><\/pre>\n<p>\u7701\u7565 <code>return<\/code> \u662f Rust \u7684\u4e00\u79cd\u60ef\u7528\u6cd5\u3002<\/p>\n<p>\u5728 Rust \u4e2d\uff0c\u533a\u95f4\u6709\u4e24\u79cd\u5e38\u7528\u7684\u8868\u793a\u65b9\u6cd5\uff1a<\/p>\n<ol>\n<li><strong><code>1..100<\/code><\/strong>\uff1a\u4e0d\u5305\u62ec\u4e0a\u9650\uff0c\u8868\u793a\u4ece 1 \u5230 99\u3002<\/li>\n<li><strong><code>1..=100<\/code><\/strong>\uff1a\u5305\u62ec\u4e0a\u9650\uff0c\u8868\u793a\u4ece 1 \u5230 100\u3002<\/li>\n<\/ol>\n<p>\u4f7f\u7528 <code>..=<\/code> \u53ef\u4ee5\u786e\u4fdd\u4e0a\u9650\u503c\u88ab\u5305\u542b\u5728\u5faa\u73af\u4e2d\u3002<\/p>\n<h2>\u5728 Rust \u4e2d\uff0c\u6709\u51e0\u79cd\u5e38\u89c1\u7684\u51fd\u6570\u5199\u6cd5\u548c\u6982\u5ff5\uff1a<\/h2>\n<h3>\u666e\u901a\u51fd\u6570<\/h3>\n<pre><code class=\"language-rust\">fn greet(name: &amp;str) {\n    println!(&quot;Hello, {}!&quot;, name);\n}<\/code><\/pre>\n<h3>\u5e26\u8fd4\u56de\u503c\u7684\u51fd\u6570<\/h3>\n<p>\u4e0d\u4f7f\u7528 <code>return<\/code> \u5173\u952e\u5b57\uff1a<\/p>\n<pre><code class=\"language-rust\">fn add(x: i32, y: i32) -&gt; i32 {\n    x + y\n}<\/code><\/pre>\n<h3>\u5e26\u6709\u53ef\u53d8\u53c2\u6570\u7684\u51fd\u6570<\/h3>\n<p>\u4f7f\u7528 <code>mut<\/code> \u5173\u952e\u5b57\uff1a<\/p>\n<pre><code class=\"language-rust\">fn increment(x: &amp;mut i32) {\n    *x += 1;\n}<\/code><\/pre>\n<h3>\u51fd\u6570\u91cd\u8f7d\uff08\u4e0d\u652f\u6301\uff09<\/h3>\n<p>Rust \u4e0d\u652f\u6301\u51fd\u6570\u91cd\u8f7d\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u6cdb\u578b\u6216\u4e0d\u540c\u7684\u53c2\u6570\u7ed3\u6784\u5b9e\u73b0\u7c7b\u4f3c\u529f\u80fd\u3002<\/p>\n<h3>\u6cdb\u578b\u51fd\u6570<\/h3>\n<pre><code class=\"language-rust\">fn largest&lt;T: PartialOrd&gt;(list: &amp;[T]) -&gt; &amp;T {\n    let mut largest = &amp;list[0];\n    for item in list {\n        if item &gt; largest {\n            largest = item;\n        }\n    }\n    largest\n}<\/code><\/pre>\n<h3>\u95ed\u5305<\/h3>\n<p>\u95ed\u5305\u662f\u53ef\u4ee5\u6355\u83b7\u73af\u5883\u7684\u533f\u540d\u51fd\u6570\uff1a<\/p>\n<pre><code class=\"language-rust\">fn main() {\n    let add_one = |x: i32| x + 1;\n    println!(&quot;{}&quot;, add_one(5));\n}<\/code><\/pre>\n<h3>\u65b9\u6cd5<\/h3>\n<p>\u5728\u7ed3\u6784\u4f53\u4e0a\u5b9a\u4e49\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-rust\">struct Rectangle {\n    width: u32,\n    height: u32,\n}\n\nimpl Rectangle {\n    fn area(&amp;self) -&gt; u32 {\n        self.width * self.height\n    }\n}<\/code><\/pre>\n<h3>\u5173\u8054\u51fd\u6570<\/h3>\n<p>\u7c7b\u4f3c\u4e8e\u9759\u6001\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-rust\">impl Rectangle {\n    fn new(width: u32, height: u32) -&gt; Rectangle {\n        Rectangle { width, height }\n    }\n}<\/code><\/pre>\n<p>\u8fd9\u4e9b\u662f Rust \u4e2d\u4e00\u4e9b\u5e38\u7528\u7684\u51fd\u6570\u5199\u6cd5\u548c\u6982\u5ff5\u3002\u5e0c\u671b\u5bf9\u4f60\u6709\u5e2e\u52a9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Rust \u7b80\u4ecb Rust \u662f\u4e00\u95e8\u4e13\u6ce8\u4e8e\u5b89\u5168\u6027\u548c\u6027\u80fd\u7684\u7cfb\u7edf\u7f16\u7a0b\u8bed\u8a00\u3002\u5b83\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u7f16\u5199\u9ad8\u6548\u3001\u5b89\u5168\u7684\u4ee3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1601,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[80],"class_list":["post-1600","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-learn","tag-rust"],"_links":{"self":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/1600","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/comments?post=1600"}],"version-history":[{"count":1,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/1600\/revisions"}],"predecessor-version":[{"id":1602,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/1600\/revisions\/1602"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/media\/1601"}],"wp:attachment":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/media?parent=1600"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/categories?post=1600"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/tags?post=1600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}