Import the Num Crate
Rust is designed to be very lightweight and provides very little right out of the box.
Within the ecosystem, many functions and features which you might expect to be included into Rust std
or core
are actually delegated to small, well-known, and widely used crates.
For our next step, we want to access traits for basic numerical operations like:
CheckedAdd
- A type which supportschecked_add
CheckedSub
- A type which supportschecked_sub
Zero
- A type which can return the value zero when callingzero()
One
- A type which can return the value one when callingone()
To access these traits, we will need to import a new crate into our project.
Cargo.toml
When we first initialized our project, a Cargo.toml
file was generated for us.
As mentioned before, it is very similar to a package.json
file you would expect to find in a Node.js project.
Already in your Cargo.toml
is metadata like the name
of your project, the version
of the crate you are building, and the edition
of Rust you are using.
What you can see is that you can also add dependencies
to your crate which will allow you to use other external crates and libraries in your project.
You can add the dependency by hand by editing your Cargo.toml
file or you can run cargo add num
.
Crates.io
Where is this crate coming from?
The Rust community has a large registry of available crates on crates.io. When you import a crate, it will use crates.io
by default.
You can also import crates directly from github by specifying the repo where the source code can be found.
That would look something like:
[dependencies]
pallet-balances = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
Add the Num Crate to Your Project
This step is short and simple.
Run cargo add num
in your project directory and make sure your project compiles afterward.
You should see something like:
➜ rust-state-machine git:(master) ✗ cargo add num
Updating crates.io index
Adding num v0.4.1 to dependencies.
Features:
+ std
- alloc
- libm
- num-bigint
- rand
- serde
Updating crates.io index
➜ rust-state-machine git:(master) ✗ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/rust-state-machine`
[package]
name = "rust-state-machine"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
num = "0.4.1"
diff --git a/Cargo.lock b/Cargo.lock
index 75fe297f..10ded84b 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2,6 +2,91 @@
# It is not intended for manual editing.
version = 3
+[[package]]
+name = "autocfg"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
+
+[[package]]
+name = "num"
+version = "0.4.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b05180d69e3da0e530ba2a1dae5110317e49e3b7f3d41be227dc5f92e49ee7af"
+dependencies = [
+ "num-bigint",
+ "num-complex",
+ "num-integer",
+ "num-iter",
+ "num-rational",
+ "num-traits",
+]
+
+[[package]]
+name = "num-bigint"
+version = "0.4.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "608e7659b5c3d7cba262d894801b9ec9d00de989e8a82bd4bef91d08da45cdc0"
+dependencies = [
+ "autocfg",
+ "num-integer",
+ "num-traits",
+]
+
+[[package]]
+name = "num-complex"
+version = "0.4.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1ba157ca0885411de85d6ca030ba7e2a83a28636056c7c699b07c8b6f7383214"
+dependencies = [
+ "num-traits",
+]
+
+[[package]]
+name = "num-integer"
+version = "0.1.45"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
+dependencies = [
+ "autocfg",
+ "num-traits",
+]
+
+[[package]]
+name = "num-iter"
+version = "0.1.43"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7d03e6c028c5dc5cac6e2dec0efda81fc887605bb3d884578bb6d6bf7514e252"
+dependencies = [
+ "autocfg",
+ "num-integer",
+ "num-traits",
+]
+
+[[package]]
+name = "num-rational"
+version = "0.4.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0"
+dependencies = [
+ "autocfg",
+ "num-bigint",
+ "num-integer",
+ "num-traits",
+]
+
+[[package]]
+name = "num-traits"
+version = "0.2.17"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c"
+dependencies = [
+ "autocfg",
+]
+
[[package]]
name = "rust-state-machine"
version = "0.1.0"
+dependencies = [
+ "num",
+]
diff --git a/Cargo.toml b/Cargo.toml
index c7fe36cd..42b80bf8 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -6,3 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
+num = "0.4.1"