25  Encoding a geohash

Objective

Modify gh_encode() so that it returns a geohash.

In the previous exercise we printed the Coord struct. Now, let’s continue the function by calling the encode() function from Rust.

encode()

The geohash::encode() function has the following signature:

pub fn encode(c: Coord, len: usize) -> Result<String, GeohashError>

Instead of returning a String we get a Result<>. In Rust, errors are data and should be handled.

For now, we’re going to ignore the possibility that an error may ever occur (we’re perfect anyways, right? 💅). Results are actually a special type of enum with the structure of

enum Result {
    Ok(T),
    Err(E)
}

Each variant of a result holds some data. If the Result was okay (meaning no error), it contains the expected type. Otherwise, it returns the specified error type.

In Rust, we can .unwrap() or .expect("error message when expectation is unmet") both Option<T> and Result<T>. So, for this case, we will do just that.

Exercise

  • Add an argument length: usize to gh_encode()
  • Specify the return type to be String
  • Use geohash::encode() on the constructed Coord with the specified length
  • Unwrap the result and return the inner String
View solution

Modify your src/rust/src/lib.rs file to have:

#[extendr]
fn gh_encode(x: f64, y: f64, length: usize) -> String {
    let coord = Coord { x, y };
    encode(coord, length).unwrap()
}

Run rextendr::document() and devtools::load_all() to register the changes to your function.

Run gh_encode(3.0, 0.14, 10L) with your new function.