Linked List

A personal knowledge base

Rust Revelations

#Failure to Build OpenSSL Crate on Mac OS X

src/openssl_shim.c:1:10: fatal error: 'openssl/hmac.h' file not found
#include <openssl/hmac.h>
1 error generated.

With OpenSSL installed via Homebrew run cargo build as follows:

OPENSSL_INCLUDE_DIR=/usr/local/opt/openssl/include OPENSSL_LIB_DIR=/usr/local/opt/openssl/lib cargo build

Note: you will need to run cargo clean before attempting to build with the openssl options set.

#How to Read the Documentation

# rust
pub struct HttpDate(pub tm);

This is a tuple type. Because tuple elements don't have a name, they can only be accessed by pattern-matching or by using N directly as a field to access the Nth element.

There is one case when a tuple struct is very useful, though, and that’s a tuple struct with only one element. We call this the ‘newtype’ pattern, because it allows you to create a new type, distinct from that of its contained value and expressing its own semantic meaning. —

#How to Read Errors

Given the struct:

# rust
struct FetchedFeed {
    feed_id: i32,
    data: String,
    content_type: Mime,
    etag: Option<EntityTag>,
    last_modified: Option<Timespec>,

and this error:

src/ 193:41 error: mismatched types:
 expected `core::option::Option<time::Timespec>`,
    found `core::option::Option<hyper::header::shared::httpdate::HttpDate>`
(expected struct `time::Timespec`,
    found struct `hyper::header::shared::httpdate::HttpDate`) [E0308]
src/             last_modified: last_modified,

src/ 226:24 error: cannot move out of borrowed content
src/         let etag =|tag| tag.to_string());

self.etag is an Option and map consumes the Option, which can't be done because it is borrowed from the struct.

#String Representation

to_string(&self) -> String[−]

Converts the value of self to an owned string


impl<T> ToString for T where T: Display + ?Sized

Use to_owned to convert a string literal (&str) into a String.

Update 2016-06-10: See also and

#Variadic Functions

Can't be done in rust (without macros) at the moment.

#Unclear Return Values

# rust
// Error
src/ 34:45 error: unable to infer enough type information about `_`; type annotations required [E0282]
src/         self.redis.sadd("queues","unable to add queue");

// Fix
let _: () = self.redis.lpush(key, payload.to_json()).unwrap();

#Determine Type of Binding

Add the following to generate a compiler error that will include the type of something:

let () = something;

#Tuples for Multiple Matches

if let (Some(foo), Some(bar)) = (container.get("A"), container.get("B"))


Last modified: 01 September 2019