DEV Community

Inder from lightspeedev
Inder from lightspeedev

Posted on

5 Essential Rust Keywords You Should Know Before Learning Rust

Image description

Recently, I started learning Rust, and unlike other languages like Python or JavaScript, which are high-level languages and involve a lot of abstraction, Rust is more complex. Low-level languages like Rust, C, or C++ often use jargon-like keywords that can be mind-boggling at first.

So here are 5 keywords with simplified meanings to make your Rust learning easier:

1.) pub — It stands for “public” in Rust. By default, variables, functions, modules, and methods are private. Using pub makes them public, meaning they can be accessed from anywhere.

fn something(){} // this is a private function
pub fn something(){} // this is a public function
Enter fullscreen mode Exit fullscreen mode

2.) mod — The next one on the list is mod. It stands for "module". A module in Rust encapsulates functions, structs, enums, and implementations. A simpler way to think of modules is like classes in Python or JavaScript, but with much more functionality and flexibility.

mod my_module {
    // `pub` stands for public and `fn` for function
    pub fn say_hello() {
        println!("Hello from my_module!");
    }
}
Enter fullscreen mode Exit fullscreen mode

3.) fn — fn is the easiest; if you have used any other programming languages, you know fn stands for functions. It is used to define functions, which are reusable pieces of code.

fn hithere() {
    println!("hi there");
}
Enter fullscreen mode Exit fullscreen mode

4.) Structs & Enums — Structs stand for “structure”. To understand structs, they are used to group related data into a single unit, so all attributes are combined, preventing bugs from missing data.

Structs and enums are related, so I decided to group them together. Enums allow you to choose one value as an option from the defined group. For example, an AppleSize enum can be either Big or Small; it can't be both.

struct Car {
    name: String,
    model: u32,
    color: String,
} // The `Car` struct groups all related properties. Without it, missing data could lead to bugs.

enum AppleSize {
    Big,
    Small,
} // The `AppleSize` enum defines the options you can choose from, but you can select only one.
Enter fullscreen mode Exit fullscreen mode

5.) impl — impl stands for "implementations". It was the one that sort of confused me when I started; it felt jarring for some reason. The one-line definition I came up with is: impl is used to encapsulate functions with their enums and structs.

Now , here is the complete code to clearly demonstrate the use of all the five keywords , pub , fn , struct , enums, mod

pub mod shapes{
    // i defined a public module name shapes

    struct Rectangle{

    }
    struct Circle{

    }
    struct Square{

    }
    // a mod can have structs here Rectangle, Circle , Square are defined
    enum Rectangle{

    }
    enum Circle{

    }
    enum Square{

    }
    // it can have enums where we can define the properites from which we can choose one 

    impl Rectangle{
        fn Rect_area(){

        }
        fn Rect_perimter(){

        }
    }
    impl Square(){
        fn square_area(){

        }
        fn square_other(){

        }
    }
    // and similiarly other impl for other as well
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)