Using our library
To use our library, we need to modify our
main.rs
file to look like this.
main.rs
extern crate hello_world;
use hello_world::{generate_greeting, generate_custom_greeting};
fn main() {
println!("{}", generate_greeting());
println!("{}", generate_custom_greeting("Robert"));
}
On the first line we declare that we will be using an external library with extern crate [package_name];
at that point we can use the two functions we defined in our library.
Next we want to bring them into scope directly so we add the line
use hello_world::{generate_greeting, generate_custom_greeting};
If we didn't add that line we could still use them like this
hello_world::generate_greeting()
which is a little less nice.
When we run our program, we should now get the following output
$ cargo run
...
Hello, world!
Hello, Robert!