Create a function in Rust
The first thing we are going to write in Rust is a function.
A function is defined with the fn keyword, like this:
| |
Above our main() function, let’s create a new function called usage().
Inside it, we’ll use the same macro used to write "Hello, world!" to output the name
and a short description of our tool:
| |
Go ahead and replace <YOUR NAME> with your name if you haven’t already done
so.
Compile and run your project with cargo run -q, which will always automatically
detect changes in your source files and rebuild your project if needed:
$ cargo run -q
Hello, world!
If you don’t really care to look at the accompanying information (Finished dev
and Running lines), you can pass the -q flag to Cargo, which tells it to be
quiet:
$ cargo run -q
Hello, world!
To save space, that’s how we’ll be compiling, building, and running the code as
we make changes to it—so don’t panic when you see the -q flag.
Now that we have a function to print the banner, let’s replace the println! command
in the main function with a call to usage():
| |
Finally, let’s compile, build, and run, all in one command:
$ cargo run -q
tinymd, a markdown compiler written by <YOUR NAME>
With our usage() function scaffolded, let’s iterate and have it return a
simple value that we can write to the console window.
