{"id":3432,"date":"2023-06-03T18:34:18","date_gmt":"2023-06-03T10:34:18","guid":{"rendered":"http:\/\/192.168.31.200\/?p=3432"},"modified":"2023-06-03T18:34:19","modified_gmt":"2023-06-03T10:34:19","slug":"rust-basic-grammar","status":"publish","type":"post","link":"http:\/\/g1n29wqq.ipyingshe.net:5347\/?p=3432","title":{"rendered":"Rust basic grammar"},"content":{"rendered":"\n<p><strong>expression grammar<\/strong><\/p>\n\n\n\n<p>expression: literal identifier operator expression function_call tuple vector map reference range<\/p>\n\n\n\n<p>literal: integer float string boolean character<\/p>\n\n\n\n<p>identifier: identifier_name<\/p>\n\n\n\n<p>operator: arithmetic_operator logical_operator comparison_operator<\/p>\n\n\n\n<p>function_call: function_name arguments<\/p>\n\n\n\n<p>arguments: expression expression arguments<\/p>\n\n\n\n<p>tuple: ( expression , )*<\/p>\n\n\n\n<p>vector: [ expression , ]*<\/p>\n\n\n\n<p>map: { key_value_pair , }<\/p>\n\n\n\n<p>key_value_pair: identifier : expression<\/p>\n\n\n\n<p>reference: &amp; expression<\/p>\n\n\n\n<p>range: expression .. expression<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">123 \/\/ integer literal<br>1.23 \/\/ float literal<br>\"hello world\" \/\/ string literal<br>true \/\/ boolean literal<br>'a' \/\/ character literal<br>\u200b<br>add_one = |x| x + 1 \/\/ function declaration<br>add_one(10) \/\/ function call<br>\u200b<br>(1, 2, 3) \/\/ tuple literal<br>[1, 2, 3] \/\/ vector literal<br>{ 1: \"one\", 2: \"two\" } \/\/ map literal<br>&amp;10 \/\/ reference to the value 10<br>1..10 \/\/ range from 1 to 10<br>\u200b<br>let number: i32 = 10; \/\/ Signed integer<br>let another_number: u32 = 20; \/\/ Unsigned integer<br>\u200b<br>let decimal_number: f32 = 1.5; \/\/ Single-precision float<br>let another_decimal_number: f64 = 2.3; \/\/ Double-precision float<br>\u200b<br>let is_even: bool = true; \/\/ Boolean value `true`<br>let is_odd: bool = false; \/\/ Boolean value `false`<br>\u200b<br>let character: char = 'a';<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>rust control grammar example<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">let number = 10;<br>\u200b<br>if number &gt; 5 {<br> &nbsp; &nbsp;println!(\"The number is greater than 5.\");<br>} else if number &lt; 5 {<br> &nbsp; &nbsp;println!(\"The number is less than 5.\");<br>} else {<br> &nbsp; &nbsp;println!(\"The number is equal to 5.\");<br>}<br>\/\/The number is greater than 5.<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">rust while grammar example<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">fn main() {<br> &nbsp; &nbsp;let mut i = 0;<br> &nbsp; &nbsp;while i &lt; 10 {<br> &nbsp; &nbsp; &nbsp; &nbsp;println!(\"The number is {}\", i);<br> &nbsp; &nbsp; &nbsp; &nbsp;i += 1;<br> &nbsp;  }<br>}<\/pre>\n\n\n\n<p>In Rust, the <code>while<\/code> loop can be used to iterate over a collection of items. For example, the following code will print the names of all the files in the current directory:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">fn main() {<br> &nbsp; &nbsp;for file in std::fs::read_dir(\".\").unwrap() {<br> &nbsp; &nbsp; &nbsp; &nbsp;let file = file.unwrap();<br> &nbsp; &nbsp; &nbsp; &nbsp;println!(\"The file is {}\", file.file_name().unwrap().to_str().unwrap());<br> &nbsp;  }<br>}<br>\/\/-------------------<br>for i in 1..10 {<br> &nbsp; &nbsp;println!(\"{}\", i);<br>}<br>\/\/select<br>\u200b<br>let x = 10;<br>let y = 20;<br>\u200b<br>let z = select(x, y) {<br> &nbsp; &nbsp;when x &gt; y =&gt; x,<br> &nbsp; &nbsp;when x &lt; y =&gt; y,<br> &nbsp; &nbsp;otherwise =&gt; 0,<br>};<br>\u200b<br>println!(\"{}\", z);<br>\u200b<br>\/\/loop grammar<br>loop {<br> &nbsp; &nbsp;println!(\"{}\", i);<br> &nbsp; &nbsp;i += 1;<br> &nbsp; &nbsp;if i &gt; 10 {<br> &nbsp; &nbsp; &nbsp; &nbsp;break;<br> &nbsp;  }<br>}<br>\u200b<\/pre>\n\n\n\n<p>Rust match grammar example<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">let number = 10;<br>\u200b<br>match number {<br> &nbsp; &nbsp;1 =&gt; println!(\"One\"),<br> &nbsp; &nbsp;2 =&gt; println!(\"Two\"),<br> &nbsp; &nbsp;3 =&gt; println!(\"Three\"),<br> &nbsp; &nbsp;_ =&gt; println!(\"Other\"),<br>}<br>\u200b<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">rust how to write data to fle in example<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">use std::fs::File;<br>use std::io::Write;<br>\u200b<br>fn main() {<br> &nbsp; &nbsp;let mut file = File::create(\"data.txt\").expect(\"Could not create file\");<br>\u200b<br> &nbsp; &nbsp;file.write_all(\"Hello, world!\".as_bytes()).expect(\"Could not write to file\");<br>}<br>\u200b<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">rust structure data type example<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">struct Person {<br> &nbsp; &nbsp;name: String,<br> &nbsp; &nbsp;age: u8,<br>}<br>\u200b<br>fn main() {<br> &nbsp; &nbsp;let person = Person {<br> &nbsp; &nbsp; &nbsp; &nbsp;name: String::from(\"John Doe\"),<br> &nbsp; &nbsp; &nbsp; &nbsp;age: 30,<br> &nbsp;  };<br>\u200b<br> &nbsp; &nbsp;println!(\"Person name: {}\", person.name);<br> &nbsp; &nbsp;println!(\"Person age: {}\", person.age);<br>}<br>\u200b<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">rust grammar cheetsheet<\/h3>\n\n\n\n<h1 class=\"wp-block-heading\">Rust Grammar Cheatsheet<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">Expressions<\/h2>\n\n\n\n<p>Expressions are evaluated to produce a value.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Literals: Integers, floats, strings, booleans, and characters<\/li>\n\n\n\n<li>Identifiers: Names of variables, functions, and types<\/li>\n\n\n\n<li>Operators: Arithmetic, logical, and comparison operators<\/li>\n\n\n\n<li>FunctionCalls: Calls to functions with arguments<\/li>\n\n\n\n<li>Tuples: Collections of values<\/li>\n\n\n\n<li>Vectors: Mutable collections of values<\/li>\n\n\n\n<li>Maps: Associative arrays of keys and values<\/li>\n\n\n\n<li>References: References to values<\/li>\n\n\n\n<li>Ranges: Sequences of values<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Statements<\/h2>\n\n\n\n<p>Statements are executed to perform actions.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Declaration: Declares a variable, function, or type<\/li>\n\n\n\n<li>Assignment: Assigns a value to a variable<\/li>\n\n\n\n<li>ExpressionStatement: Evaluates an expression and produces a value<\/li>\n\n\n\n<li>IfStatement: Executes a block of code if a condition is met<\/li>\n\n\n\n<li>ElseStatement: Executes a block of code if a condition is not met<\/li>\n\n\n\n<li>LoopStatement: Executes a block of code repeatedly<\/li>\n\n\n\n<li>BreakStatement: Breaks out of the current loop<\/li>\n\n\n\n<li>ContinueStatement: Continues to the next iteration of the current loop<\/li>\n\n\n\n<li>ReturnStatement: Returns from the current function<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Control Flow<\/h2>\n\n\n\n<p>Control flow statements are used to control the order in which statements are executed.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>IfStatement: Executes a block of code if a condition is met<\/li>\n\n\n\n<li>ElseStatement: Executes a block of code if a condition is not met<\/li>\n\n\n\n<li>LoopStatement: Executes a block of code repeatedly<\/li>\n\n\n\n<li>BreakStatement: Breaks out of the current loop<\/li>\n\n\n\n<li>ContinueStatement: Continues to the next iteration of the current loop<\/li>\n\n\n\n<li>ReturnStatement: Returns from the current function<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Functions<\/h2>\n\n\n\n<p>Functions are blocks of code that can be called from other code.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>FunctionDeclaration: Defines a function<\/li>\n\n\n\n<li>FunctionCall: Calls a function with arguments<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Types<\/h2>\n\n\n\n<p>Types are used to define the data that can be stored in variables and expressions.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Scalar types: Integers, floats, strings, booleans, and characters<\/li>\n\n\n\n<li>Compound types: Tuples, vectors, maps, references, and ranges<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Modules<\/h2>\n\n\n\n<p>Modules are used to organize code into logical units.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>ModuleDeclaration: Defines a module<\/li>\n\n\n\n<li>ImportStatement: Imports a module into the current scope<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Crates<\/h2>\n\n\n\n<p>Crates are self-contained units of Rust code that can be compiled and used independently.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>CrateDeclaration: Defines a crate<\/li>\n\n\n\n<li>Cargo.toml: File that specifies the dependencies and metadata for a crate<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Cargo<\/h2>\n\n\n\n<p>Cargo is the build system for Rust. It is used to compile, run, and test Rust code.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Cargo.toml: File that specifies the dependencies and metadata for a crate<\/li>\n\n\n\n<li>cargo build: Compiles a crate<\/li>\n\n\n\n<li>cargo run: Runs a crate<\/li>\n\n\n\n<li>cargo test: Tests a crate<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>expression grammar expression: literal i <span class=\"readmore\"><a href=\"http:\/\/g1n29wqq.ipyingshe.net:5347\/?p=3432\">Continue Reading<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-3432","post","type-post","status-publish","format-standard","hentry","category-6"],"_links":{"self":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3432","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3432"}],"version-history":[{"count":1,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3432\/revisions"}],"predecessor-version":[{"id":3433,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3432\/revisions\/3433"}],"wp:attachment":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3432"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3432"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}