Hello everyone π
In this article, I will talk about the main points of documenting code in Rust.
Rust provides the rustdoc tool out of the box. Its main task is to generate documentation for a Rust project. If it's simple, then it takes the corresponding Markdown comments to the code and the code itself and converts them into a beautiful HTML-CSS-JS representation, into a website.
You can call the rustdoc command directly, or using cargo. I prefer the second option.
Go to your working directory, open a terminal window in it and create a new Rust project:
cargo new --lib rust_doc && cd rust_doc
Run the following command in the term:
cargo doc --open
In the browser window that opens, you will see a basic, so far empty, site with documentation for your project. Let's write some code. Open the rust_doc project in your favorite editor, personally I work in Visual Code, and then open the file lib.rs , which is located in the src folder.
Let's create a file first lib.rs the Employee structure and define the basic initialization function new for it, which takes the employee's first name, last name and position at the input, and returns the instance of the Employee structure:
pub struct Employee {
pub name: String,
pub lastname: String,
pub job_title: String,
}
impl Employee {
pub fn new(name: String, lastname: String, job_title: String) -> Person {
return Employee {
name,
lastname,
job_title,
};
}
}
Now we can start documenting the code.
Add to the very beginning of the file lib.rs the following code snippet, then run cargo doc --open: in the console:
//! # rust_doc lib
ο»Ώ//! *It's created to learn basics
ο»Ώ//! of Rust-code documenting*
