rust: Making a library crate to make stronger a Rust binary crate in the similar bundle

(*1*)

In our subcommand fit we’re going to upload a brand new serve as known as
write to take care of our Write subcommand. We’re going to go it
the argument identify and we’re going to “import” it from a library
known as digital_garden.

use digital_garden::write; fn primary() -> Consequence<()> { color_eyre::set up()?; let decide = Choose::from_args(); dbg!(&decide); fit decide.cmd { Command::Write { identify } => write(identify), }
}

To create this library, upload a brand new phase in Shipment.toml.
We will give our library a reputation and set which document to regard
because the entrypoint. src/lib.rs is the default document for
a library crate, however we will be able to even be specific about what we
need to occur.

[lib]
title = "digital_garden"
trail = "src/lib.rs"

This leaves us with two crates, a binary crate, of which we
will have any quantity, and a library crate, of which we will be able to
most effective have 0 or 1.

We’re going to create the src/lib.rs document with the next
contents.

mod write; pub use write::write;

Rust’s module device is specific, so once we write
mod write, we say that the library digital_garden
has a module write, such that the trail to the module is
digital_garden::write.

In our case digital_garden::write corresponds to a document
at src/write.rs.

Secondly, the pub use write::write says that we’ve got a
serve as known as write within the module known as write and
that the serve as shall be uncovered from lib publicly, so
individuals who have the digital_garden library put in
will be capable to use the write serve as as
digital_garden::write.

In JavaScript the nearest analogue is exporting a serve as
from any other document that exports it.

export { write } from './write.js';

Via the use of use, we are shortening the general public trail to the write
serve as. A personal module “write” with a serve as “write”
additionally exists at digital_garden::write::write. When you check out
to make use of this trail, Rust will inform you there’s a non-public
module there.

In spite of everything, in src/write.rs, we will be able to write a public serve as
that fits the go back form of our primary serve as, since
we are the use of it because the go back price from our fit serve as.

We’re going to settle for an Choice<String> as an issue as smartly,
since that is what we predict from the cli arguments.

use color_eyre::Consequence; pub fn write(_title: Choice<String>) -> Consequence<()> { todo!()
}

The aim of constructing this module trail is as a result of we
know that we are going to arrange extra modules for every
subcommand one day, and we will be able to regulate each the
placement of our code on disk, in addition to the trail customers
will have interaction with to make use of our module.