All Projects → ankur-anand → five-min-rust

ankur-anand / five-min-rust

Licence: BSD-3-Clause license
Rust concise 5 Minutes cheat-sheet for each concepts. (WIP)

Programming Languages

rust
11053 projects

Projects that are alternatives of or similar to five-min-rust

guitar-tabs-to-MIDI
A program that converts Guitar Tabs into MIDI files.
Stars: ✭ 38 (+137.5%)
Mutual labels:  notes
MusicTheory
Music Theory Library for Java and Android apps
Stars: ✭ 24 (+50%)
Mutual labels:  notes
zone
A URL shortener / note sharing service.
Stars: ✭ 18 (+12.5%)
Mutual labels:  notes
compsci
Lecture notes, projects, and more resources on the courses I attended for my Bachelor's and Master's degrees in Computer Science
Stars: ✭ 15 (-6.25%)
Mutual labels:  notes
gtd.vim
Getting things done with Vim
Stars: ✭ 42 (+162.5%)
Mutual labels:  notes
notes
📖 What I'm reading lately
Stars: ✭ 85 (+431.25%)
Mutual labels:  notes
notes
个人笔记
Stars: ✭ 24 (+50%)
Mutual labels:  notes
Road2StrategyPM
产品策划开发修炼手记 👩🏻‍💻
Stars: ✭ 80 (+400%)
Mutual labels:  notes
pinotes
Self hosted notes for Raspberry Pi
Stars: ✭ 36 (+125%)
Mutual labels:  notes
side-notes
Easy access to a notes file in Emacs
Stars: ✭ 19 (+18.75%)
Mutual labels:  notes
onepile
Playground for the future of private notes and document management
Stars: ✭ 41 (+156.25%)
Mutual labels:  notes
til
Personal Wiki of Interesting things I learn every day at the intersection of software, life & stuff aka my second brain 🧠️
Stars: ✭ 41 (+156.25%)
Mutual labels:  notes
garden
A digital garden prototype - my notes and links are here
Stars: ✭ 23 (+43.75%)
Mutual labels:  notes
MLOps-Specialization-Notes
Notes for Machine Learning Engineering for Production (MLOps) Specialization course by DeepLearning.AI & Andrew Ng
Stars: ✭ 143 (+793.75%)
Mutual labels:  notes
GitNotes
一个在浏览器上运行的笔记浏览应用, 用于浏览以 Markdown 书写的, 存放在 GitLab 或 GitHub 上的笔记.
Stars: ✭ 33 (+106.25%)
Mutual labels:  notes
electron-vue3-inote
使用electron11+vue3.x+ts的桌面端便笺项目,拥有漂亮的过渡动画效果,以富文本形式储存在本地,可多开输入窗口。(The desktop note project using electron11+vue3.x+ts has beautiful transition animation effects, stored locally in the form of rich text, and can open more input windows.)
Stars: ✭ 168 (+950%)
Mutual labels:  notes
NoteCrypt
Keep your notes safe and secure with Note Crypt for Android!
Stars: ✭ 32 (+100%)
Mutual labels:  notes
NoteUsingRoom
Note Apps using Room database
Stars: ✭ 18 (+12.5%)
Mutual labels:  notes
rofi-notes
📓 tiny script for managing notes with rofi
Stars: ✭ 13 (-18.75%)
Mutual labels:  notes
jot
Command-line note-taking for minimalists
Stars: ✭ 24 (+50%)
Mutual labels:  notes

Five minutes Rust

Rust concise 5 Minutes cheat-sheet for each concepts.


Hello, Rust!

fn main() {
    println!("Hi There!{}", "Ankur Anand");
}

Important println! is not an function. It's a macro in rust. (macro end with !)


Macros!

  • Macros are like functions, but they're named with ! at the end.
  • Can do generally very powerful stuff.
    • They actually generate code at compile time!
    • Think macro in c but more hygienic. More later!
  • Call and use macros like functions.
  • You can define your own with macro_rules! macro_name blocks.
    • These are very complicated. More later!
  • Because they're so powerful, a lot of common utilities are defined as macros.
  • println! macro

print! & println!

  • Print stuff out. Yay.
  • Use {} for general string interpolation, and {:?} for debug printing.
    • Some types can only be printed with {:?}, like arrays and Vecs.
print!("{}, {}, {}", "foo", 3, true);
// => foo, 3, true
println!("{:?}, {:?}", "foo", [1, 2, 3]);
// => "foo", [1, 2, 3]

format!

  • Uses println!-style string interpolation to create formatted Strings.
let fmted = format!("{}, {:x}, {:?}", 12, 155, Some("Hello"));
// fmted == "12, 9b, Some("Hello")"

Variable Bindings

How to store a value and refer it later ?

In rust we can decalare a variable with let keyword. i.e Variables are bound with let

let name = "ankur";
  • Bindings are implicitly-typed: the compiler infers based on context.
  • The compiler can't always determine the type of a variable, so sometimes you have to add type annotations.
let x: i16 = 9;

All the variables are immutable by default in Rust. i.e Once the variable has been decalared and initialized, you cannot assign some other value to it by default.

Trying to reassign will throw you an error at compile time.

fn main() {
    let x_immutable = 10;
    println!("x value : {}", x_immutable);
    x_immutable = 20;
}
error[E0384]: cannot assign twice to immutable variable `x_immutable`
 --> var.rs:4:5
  |
2 |     let x_immutable = 10;
  |         ----------- first assignment to `x_immutable`
3 |     println!("x value : {}", x_immutable);
4 |     x_immutable = 20;
  |     ^^^^^^^^^^^^^^^^ cannot assign twice to immutable variable

So if We want to reassign variable to some other value, add mut keyword before it.

fn main() {
    let mut y_mutable = 20;
    println!("y mut {}", y_mutable);
    y_mutable = 30;
    println!("y mut {}", y_mutable);
}
  • Bindings may be shadowed:
let x = 17;
let y = 53;
let x = "Shadowed!";
// x is not mutable, but we're able to re-bind it
  • The shadowed binding for x above lasts until it goes out of scope.

  • Above, we've effectively lost the first binding, since both xs are in the same scope.

  • Patterns may also be used to declare variables:

let (a, b) = ("foo", 12);

Expressions

Expression are something that returns a value

  • Almost everything is an expression: in Rust. Except variable bindings

  • The "nothing" type is called "unit", which is written ().

    • The type () has only one value: ().
    • () is the default return type.
  • Discard an expression's value by appending a semicolon. Now it returns ().

    • Hence, if a function ends in a semicolon, it returns ().
fn foo() -> i32 { 5 }
fn bar() -> () { () }
fn baz() -> () { 5; }
fn qux()       { 5; }
  • Because everything is an expression, we can bind many things to variable names:
let x = -5;
let y = if x > 0 { "greater" } else { "less" };
println!("x = {} is {} than zero", x, y);
  • Aside: "{}" is Rust's (most basic) string interpolation operator
    • like printf's "%s" in C/C++.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].