// Positional Arguments println!("{0} is {1} {2}, also {0} is a {3} programming language", "Rust", "cool", "language", "safe");
// Named Arguments println!("{country} is an diverse nation with unity.", country = "India");
// Placeholder traits :b for binary, :0x is for hex and :o is octal println!("Let us print 76 is binary which is {:b} , and hex equivalent is {:0x} and octal equivalent is {:o}", 76, 76, 76);
// Debug Trait println!("Print whatever we want to here using debug trait {:?}", (76, 'A', 90));
// New Format Strings in 1.58 let x = "world"; println!("Hello {x}!");
Printing Styles
1 2 3 4 5 6 7 8 9 10 11
// Prints the output print!("Hello World\n");
// Appends a new line after printing println!("Appending a new line");
// Prints as an error eprint!("This is an error\n");
// Prints as an error with new line eprintln!("This is an error with new line");
Variables
1 2 3 4 5 6 7 8 9 10 11
// Initializing and declaring a variable let some_variable = "This_is_a_variable";
// Making a variable mutable letmut mutable_variable = "Mutable";
// Assigning multiple variables let (name, age) = ("ElementalX", 20);
letmut hi = String::from("Hey there..."); hi.push_str("How are you doing??");
// => Hey there...How are you doing?? println!("{hi}");
Rust Operators {.cols-3}
Comparison Operators
e == f
e is equal to f
e != f
e is NOT equal to f
e < f
e is less than f
e > f
e is greater f
e <= f
e is less than or equal to f
e >= f
e is greater or equal to f
1 2 3 4 5 6 7 8
let (e, f) = (1, 100);
let greater = f > e; // => true let less = f < e; // => false let greater_equal = f >= e; // => true let less_equal = e <= f; // => true let equal_to = e == f; // => false let not_equal_to = e != f; // => true
Arithmetic Operators
a + b
a is added to b
a - b
b is subtracted from a
a / b
a is divided by b
a % b
Gets remainder of a by dividing with b
a * b
a is multiplied with b
{.wrap}
1 2 3 4 5 6 7
let (a, b) = (4, 5);
let sum: i32 = a + b; // => 9 let subtractions: i32 = a - b; // => -1 let multiplication: i32 = a * b; // => 20 let division: i32 = a / b; // => 0 let modulus: i32 = a % b; // => 4
Bitwise Operators
Operator
Description
g & h
Binary AND
`g
h`
g ^ h
Binary XOR
g ~ h
Binary one’s complement
g << h
Binary shift left
g >> h
Binary shift right
{.wrap}
1 2 3 4 5 6 7
let (g, h) = (0x1, 0x2);
let bitwise_and = g & h; // => 0 let bitwise_or = g | h; // => 3 let bitwise_xor = g ^ h; // => 3 let right_shift = g >> 2; // => 0 let left_shift = h << 4; // => 32
Logical Operators
Example
Meaning
c && d
Both are true (AND)
c OR d
Either is true (OR)
!c
c is false (NOT)
1 2 3 4 5
let (c, d) = (true, false);
let and = c && d; // => false let or = c || d; // => true let not = !c; // => false
Compound Assignment Operator
1 2
letmut k = 9; letmut l = k;
Operator
Description
k += l
Add a value and assign, then k=9
k -= l
Substrate a value and assign, then k=18
k /= l
Divide a value and assign, then k=9
k *= l
Multiply a value and assign, then k=81
`k
= l`
Rust Flow Control {.cols-3}
If Expression
1 2 3 4 5 6
let case1: i32 = 81; let case2: i32 = 82;
if case1 < case2 { println!("case1 is greater than case2"); }
If…Else Expression
1 2 3 4 5 6 7 8
let case3 = 8; let case4 = 9;
if case3 >= case4 { println!("case3 is better than case4"); } else { println!("case4 is greater than case3"); }
If…Else…if…Else Expression
1 2 3 4 5 6 7 8 9 10 11 12
let foo = 12; let bar = 13;
if foo == bar { println!("foo is equal to bar"); } elseif foo < bar { println!("foo less than bar"); } elseif foo != bar { println!("foo is not equal to bar"); } else { println!("Nothing"); }
letmut i = 1; loop { println!("i is {i}"); if i > 100 { break; } i *= 2; } ```
### Continue Statement
```rust for (v, c) in (0..10+1).enumerate(){ println!("The {c} number loop"); if v == 9{ println!("Here we go continue?"); continue; } println!{"The value of v is : {v}"}; }
fnpower_of_three(by_ref: &muti32){ // de-referencing is important *by_ref = *by_ref * *by_ref; println!("{by_ref}"); // => 9 }
Returns
{.wrap}
1 2 3 4 5 6 7 8 9 10 11 12 13 14
fnmain(){ let (mut radius, mut pi) = (3.0, 3.14); let(area, _perimeter) = calculate ( &mut radius, &mut pi ); println!("The area and the perimeter of the circle are: {area} & {_perimeter}"); }
fncalculate(radius : &mutf64, pi: &mutf64) -> (f64, f64){ let perimeter = 2.0 * *pi * *radius; let area = *pi * *radius * *radius; return (area, perimeter); }