23  Adding dependencies

Objective

Add a dependency to our package and import items from it.

Dependencies in Rust are managed by the Cargo.toml file. R will let you use packages that are on your machine even if you do not explicitly declare them as dependencies. Rust will not. You must declare dependencies.

Cargo.toml

The Cargo.toml is akin to the DESCRIPTION file. Dependencies are set via the [dependencies] table.

We can add dependencies manually, using cargo add crate-name, or via rextendr::use_crate("cratename").

[package]
name = "geohash"
publish = false
version = "0.1.0"
edition = "2021"
rust-version = "1.65"

[lib]
crate-type = [ "staticlib" ]
name = "geohashrs"

[dependencies]
extendr-api = "*"

Rust dependencies use semantic versionining (semver). The "*" is a shorthand for specifying the latest stable release.

Importing items from dependencies

When dependencies are added to a crate, the items from the dependency are accessed via namespace similar to R. If we look at lib.rs we see at the top

use extendr_api::prelude::*;

This has the format of crate_name::module::item. The * is a wildcard for importing everything from the module.

The prelude is a common module name. It re-exports the commonly used items from a crate all together so you do not have to manually list them.

Multiple items can be imported using {} for example:

use crate_name::{StructName, function_name};

Exercise

  • Add the geohash crate as a dependency
  • Import the Coord struct and the encode function from geohash in lib.rs
View solution

From the R terminal run:

rextendr::use_crate("geohash")

This will add the geohash dependency.

Open src/rust/src/lib.rs and add the following below use extendr_api::prelude::*;

use geohash::{Coord, encode};