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

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. — https://doc.rust-lang.org/book/structs.html#tuple-structs

#How to Read Errors

Given the struct:

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

and this error:

src/main.rs:193:28: 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/main.rs:193             last_modified: last_modified,
                                           ^~~~~~~~~~~~~


src/main.rs:226:20: 226:24 error: cannot move out of borrowed content
src/main.rs:226         let etag = self.etag.map(|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.

http://doc.rust-lang.org/1.0.0-beta.4/core/option/enum.Option.html#method.as_ref

#String Representation

to_string(&self) -> String[−]

Converts the value of self to an owned string

Implementors:

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 http://www.suspectsemantics.com/blog/2016/03/27/string-types-in-rust/ and https://users.rust-lang.org/t/to-string-vs-to-owned-for-string-literals/1441/5

#Variadic Functions

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

#Unclear Return Values

// Error
src/background.rs:34:20: 34:45 error: unable to infer enough type information about `_`; type annotations required [E0282]
src/background.rs:34         self.redis.sadd("queues", self.name).ok().expect("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"))

Source: https://www.reddit.com/r/rust/comments/4lnxtk/comment/d3u8r8f

Last modified: 11 Jul 2016