@@ -1,36 +1,36 @@
|
||||
# Libraries and binaries
|
||||
|
||||
It took a bit of code to implement the `Error` trait for `TicketNewError`, didn't it?
|
||||
It took a bit of code to implement the `Error` trait for `TicketNewError`, didn't it?\
|
||||
A manual `Display` implementation, plus an `Error` impl block.
|
||||
|
||||
We can remove some of the boilerplate by using [`thiserror`](https://docs.rs/thiserror/latest/thiserror/),
|
||||
a Rust crate that provides a **procedural macro** to simplify the creation of custom error types.
|
||||
But we're getting ahead of ourselves: `thiserror` is a third-party crate, it'd be our first dependency!
|
||||
We can remove some of the boilerplate by using [`thiserror`](https://docs.rs/thiserror/latest/thiserror/),
|
||||
a Rust crate that provides a **procedural macro** to simplify the creation of custom error types.\
|
||||
But we're getting ahead of ourselves: `thiserror` is a third-party crate, it'd be our first dependency!
|
||||
|
||||
Let's take a step back to talk about Rust's packaging system before we dive into dependencies.
|
||||
|
||||
## What is a package?
|
||||
|
||||
A Rust package is defined by the `[package]` section in a `Cargo.toml` file, also known as its **manifest**.
|
||||
A Rust package is defined by the `[package]` section in a `Cargo.toml` file, also known as its **manifest**.
|
||||
Within `[package]` you can set the package's metadata, such as its name and version.
|
||||
|
||||
Go check the `Cargo.toml` file in the directory of this section's exercise!
|
||||
|
||||
## What is a crate?
|
||||
|
||||
Inside a package, you can have one or more **crates**, also known as **targets**.
|
||||
Inside a package, you can have one or more **crates**, also known as **targets**.\
|
||||
The two most common crate types are **binary crates** and **library crates**.
|
||||
|
||||
### Binaries
|
||||
|
||||
A binary is a program that can be compiled to an **executable file**.
|
||||
A binary is a program that can be compiled to an **executable file**.\
|
||||
It must include a function named `main`—the program's entry point. `main` is invoked when the program is executed.
|
||||
|
||||
### Libraries
|
||||
|
||||
Libraries, on the other hand, are not executable on their own. You can't _run_ a library,
|
||||
but you can _import its code_ from another package that depends on it.
|
||||
A library groups together code (i.e. functions, types, etc.) that can be leveraged by other packages as a **dependency**.
|
||||
Libraries, on the other hand, are not executable on their own. You can't _run_ a library,
|
||||
but you can _import its code_ from another package that depends on it.\
|
||||
A library groups together code (i.e. functions, types, etc.) that can be leveraged by other packages as a **dependency**.
|
||||
|
||||
All the exercises you've solved so far have been structured as libraries, with a test suite attached to them.
|
||||
|
||||
@@ -55,7 +55,7 @@ You can use `cargo` to scaffold a new package:
|
||||
cargo new my-binary
|
||||
```
|
||||
|
||||
This will create a new folder, `my-binary`, containing a new Rust package with the same name and a single
|
||||
This will create a new folder, `my-binary`, containing a new Rust package with the same name and a single
|
||||
binary crate inside. If you want to create a library crate instead, you can use the `--lib` flag:
|
||||
|
||||
```bash
|
||||
|
||||
Reference in New Issue
Block a user