Rust basic grammar

expression grammar

expression: literal identifier operator expression function_call tuple vector map reference range

literal: integer float string boolean character

identifier: identifier_name

operator: arithmetic_operator logical_operator comparison_operator

function_call: function_name arguments

arguments: expression expression arguments

tuple: ( expression , )*

vector: [ expression , ]*

map: { key_value_pair , }

key_value_pair: identifier : expression

reference: & expression

range: expression .. expression

123 // integer literal
1.23 // float literal
"hello world" // string literal
true // boolean literal
'a' // character literal

add_one = |x| x + 1 // function declaration
add_one(10) // function call

(1, 2, 3) // tuple literal
[1, 2, 3] // vector literal
{ 1: "one", 2: "two" } // map literal
&10 // reference to the value 10
1..10 // range from 1 to 10

let number: i32 = 10; // Signed integer
let another_number: u32 = 20; // Unsigned integer

let decimal_number: f32 = 1.5; // Single-precision float
let another_decimal_number: f64 = 2.3; // Double-precision float

let is_even: bool = true; // Boolean value `true`
let is_odd: bool = false; // Boolean value `false`

let character: char = 'a';

rust control grammar example

let number = 10;

if number > 5 {
   println!("The number is greater than 5.");
} else if number < 5 {
   println!("The number is less than 5.");
} else {
   println!("The number is equal to 5.");
}
//The number is greater than 5.

rust while grammar example

fn main() {
   let mut i = 0;
   while i < 10 {
       println!("The number is {}", i);
       i += 1;
  }
}

In Rust, the while 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:

fn main() {
   for file in std::fs::read_dir(".").unwrap() {
       let file = file.unwrap();
       println!("The file is {}", file.file_name().unwrap().to_str().unwrap());
  }
}
//-------------------
for i in 1..10 {
   println!("{}", i);
}
//select

let x = 10;
let y = 20;

let z = select(x, y) {
   when x > y => x,
   when x < y => y,
   otherwise => 0,
};

println!("{}", z);

//loop grammar
loop {
   println!("{}", i);
   i += 1;
   if i > 10 {
       break;
  }
}

Rust match grammar example

let number = 10;

match number {
   1 => println!("One"),
   2 => println!("Two"),
   3 => println!("Three"),
   _ => println!("Other"),
}

rust how to write data to fle in example

use std::fs::File;
use std::io::Write;

fn main() {
   let mut file = File::create("data.txt").expect("Could not create file");

   file.write_all("Hello, world!".as_bytes()).expect("Could not write to file");
}

rust structure data type example

struct Person {
   name: String,
   age: u8,
}

fn main() {
   let person = Person {
       name: String::from("John Doe"),
       age: 30,
  };

   println!("Person name: {}", person.name);
   println!("Person age: {}", person.age);
}

rust grammar cheetsheet

Rust Grammar Cheatsheet

Expressions

Expressions are evaluated to produce a value.

  • Literals: Integers, floats, strings, booleans, and characters
  • Identifiers: Names of variables, functions, and types
  • Operators: Arithmetic, logical, and comparison operators
  • FunctionCalls: Calls to functions with arguments
  • Tuples: Collections of values
  • Vectors: Mutable collections of values
  • Maps: Associative arrays of keys and values
  • References: References to values
  • Ranges: Sequences of values

Statements

Statements are executed to perform actions.

  • Declaration: Declares a variable, function, or type
  • Assignment: Assigns a value to a variable
  • ExpressionStatement: Evaluates an expression and produces a value
  • IfStatement: Executes a block of code if a condition is met
  • ElseStatement: Executes a block of code if a condition is not met
  • LoopStatement: Executes a block of code repeatedly
  • BreakStatement: Breaks out of the current loop
  • ContinueStatement: Continues to the next iteration of the current loop
  • ReturnStatement: Returns from the current function

Control Flow

Control flow statements are used to control the order in which statements are executed.

  • IfStatement: Executes a block of code if a condition is met
  • ElseStatement: Executes a block of code if a condition is not met
  • LoopStatement: Executes a block of code repeatedly
  • BreakStatement: Breaks out of the current loop
  • ContinueStatement: Continues to the next iteration of the current loop
  • ReturnStatement: Returns from the current function

Functions

Functions are blocks of code that can be called from other code.

  • FunctionDeclaration: Defines a function
  • FunctionCall: Calls a function with arguments

Types

Types are used to define the data that can be stored in variables and expressions.

  • Scalar types: Integers, floats, strings, booleans, and characters
  • Compound types: Tuples, vectors, maps, references, and ranges

Modules

Modules are used to organize code into logical units.

  • ModuleDeclaration: Defines a module
  • ImportStatement: Imports a module into the current scope

Crates

Crates are self-contained units of Rust code that can be compiled and used independently.

  • CrateDeclaration: Defines a crate
  • Cargo.toml: File that specifies the dependencies and metadata for a crate

Cargo

Cargo is the build system for Rust. It is used to compile, run, and test Rust code.

  • Cargo.toml: File that specifies the dependencies and metadata for a crate
  • cargo build: Compiles a crate
  • cargo run: Runs a crate
  • cargo test: Tests a crate