How do keywords like "use","import","include" differ and how do they manage libraries,modules and packages?

Can any one explain me the difference between "use","import" and "include" keywords
how "use" keyword in rust differ form the "import" in java,go,nim etc.. while using libraries and modules.
Is use and import show same functionality!

In Rust, the use keyword does two main things. First, it adds an alias to a name that was defined in some other module. For example, if the module crate::foo contains a function named bar, you can call it from other modules by typing out a full path to its location:

crate::foo::bar();

or you can "use" it to create an alias so you don't need to type the full path every time:

use crate::foo::bar;
// Now `bar` in this module is an alias for `crate::foo::bar`.
bar();

Second, if you "use" a trait, you can call its methods using "dot" syntax. For example, instead of calling a method by using a full path to a trait:

std::ops::Add::add(0, 0)

you can "use" the trait and then use the shorter method call syntax:

use std::ops::Add;
// In this module I can now call methods of the `Add` trait like this:
0.add(0)

Note that both of these things are optional: You could just type out the full path and not need to use anything. The "use" items here just give you shorter ways to refer to items and methods from other modules.

In Rust, every item is defined in exactly one place. For example, the mod keyword defines a module (and "includes" the source file with the module's contents). So your program should have exactly one mod item for each module/file that it contains. But after an item has been defined, you can "use" it many times, anywhere that you want a convenient way to refer to it.

4 Likes