• Cargo installation

  • Variables learning

    • Mutable and Immutable
    • Constants
      • Const Variables cannot be mutable if we add “mut” in front of variable it will give error
      • Const variables should never be changed at any point of code. it should not be used for return type. in short we can never use const variable to change at runtime.
  • Data Types

    • Scalar : Single values
      • Integer
        • default is u32
        • it will have int over flow 256 becomes 0 and 257 become 1
      • Floating
        • default is u64
      • Boolean
      • Char
    • Compound : Group of values
      • Tuple : fixed size array of related data of diff type
      • Funcs
      • Control Flow
        • if else
        • type of loops
          • loops
          • while
          • for end
  • Ownership

    • memory & allocation

      • code

        Screenshot 2024-10-10 at 3.22.51 PM.png

      here as per normal case you will think that this s2 string will be get copied to s1 string.

      in rust s1 value will get copied to s2 but s1 value will get removed.

      so in short if you simply assign s1 = “hello”; s2 = s1 . they value of s2 will be “hello” but value of s1 will get removed. basically value of s1 is moved to s2.

      if we try to print y value will get compile time error.

      and if you want to specifically clone/copy value from x to y by keeping y then you can use s1.clone()

      but in above code case x will be copy to y. in rust i will use as special copy tray for variables like int, bool and char.

    • Takes_ownership

      • code

        fn main() {
            let s = String::from("hello");
            takes_owership(s);
            println!("{}",s);
        }
        
        fn takes_owership(some_string:String){
        
            println!("{}",some_string);
        
        }
        

        this will give error. why bcz takes_ownership func takes ownership of that string. so as per above memory & allocation logic it will give error.

        as strings s value is moved to the func some_string.

        if we use same logic for int , bool , char then as per we know this will copy the values and will get value printed.

    • Gives_ownership

      • code

        xfn main() {
            let s = gives_owership();
            println!("{}",s);
        }
        
        fn gives_owership() -> String {
            let some_string = String::from("hello");
            some_string
        }
        

        this code will work fine and give you the hello o/p as expected. as give ownership func gives ownership and returns some_string which will print in main.

    • Takes_and_gives back

      • code

        fn main() {
            let s1 = gives_owership();
            let s2 = String::from("hello");
            let s3 = takes_and_gives_back(s2);
            println!("s1:{},s3:{}",s1,s3);
        }
        
        fn gives_owership() -> String {
            let some_string = String::from("hello");
            some_string
        }
        
        fn takes_and_gives_back(a_string: String) -> String{
            a_string
        }
        
    • Referencing

      • rules of references :
        • at any given time , you can have either one mutable ref or any number of immutable ref.
        • refs must always be valid
    • Slices

      • &String —> this are strings.
      • &str —> this are slices
  • Structs

    • if we mut struct then entire struct will be mutable
    • Field init shorthand syntax
    • methods get passed to “&self” but associate funcs dont.
  • Enums and pattern matching

    • code

      image.png

    • we can create enum like this. but we can also create separate struct like below :

      image.png

    • but if we do like this we will not have same type structs with enum we can group them in same message type.

    • The Option Enum :

      • main langs use null values. null value has usedful case that it will be any value or null value.
        • but rust dont have null values so we use options.

        • for doing sum or any math we cant directly add or do math on int x and option int y.

        • we can use unwrap for this.

          Screenshot 2024-10-17 at 11.58.50 AM.png

  • Match Expression

    • Match Epx are exhaustive. means we need to match all possible values.
    • code :
      • both above are same but if else is some what confusing. it starts with “if let” then if some_value = some(3) then it will print “three”. some what diff.

        image.png

  • RUST module explained

    • Pkgs and crates

      • in rust we will have two default binary crates : main.rs and lib.rs
      • bin.rs contain binary crates
      • rules
        • pkg could have atleast one crate
        • pkg could have zero lib crates or 1 lib crate
        • pkg could have any num of binary crates
    • Modules

  • Common Collections in Rust

  • Error Handling

  • Generic Types In RUST

  • Traits

  • RUST Lifetimes

  • Testing in RUST