Skip to content

Instantly share code, notes, and snippets.

@mxhold
Created October 29, 2016 16:32
Show Gist options
  • Save mxhold/b316c71c3eaff54b4141187228b6e621 to your computer and use it in GitHub Desktop.
Save mxhold/b316c71c3eaff54b4141187228b6e621 to your computer and use it in GitHub Desktop.
Rust WTF moments

This compiles:

extern crate hyper;
use std::{thread, time};

use hyper::server::{Server, Request, Response};
use hyper::uri::RequestUri::*;

fn hello(req: Request, res: Response) {
    let path: String = match req.uri {
        AbsolutePath(string) => string,
        _ => panic!(),
    };

    println!("{}", path);

    let pref: &str = &path;

    let response_text = match pref {
        "/hello" => "Hi!",
        "/goodbye" => "See ya!",
        _ => "Welcome!",
    };

    res.send(response_text.as_bytes());
}

fn main() {
    Server::http("localhost:5000").unwrap().handle(hello).unwrap();
}

but this does not:

extern crate hyper;
use std::{thread, time};

use hyper::server::{Server, Request, Response};
use hyper::uri::RequestUri::*;

fn hello(req: Request, res: Response) {
    let path: String = match req.uri {
        AbsolutePath(string) => string,
        _ => panic!(),
    };

    println!("{}", path);

    let response_text = match &path {
        "/hello" => "Hi!",
        "/goodbye" => "See ya!",
        _ => "Welcome!",
    };

    res.send(response_text.as_bytes());
}

fn main() {
    Server::http("localhost:5000").unwrap().handle(hello).unwrap();
}

fails to compile with this error:

   Compiling pastebin_rs v0.1.0 (file:///Users/max/src/rust/pastebin_rs)
error[E0308]: mismatched types
  --> src/main.rs:16:9
   |
16 |         "/hello" => "Hi!",
   |         ^^^^^^^^ expected struct `std::string::String`, found str
   |
   = note: expected type `&std::string::String`
   = note:    found type `&'static str`

error[E0308]: mismatched types
  --> src/main.rs:17:9
   |
17 |         "/goodbye" => "See ya!",
   |         ^^^^^^^^^^ expected struct `std::string::String`, found str
   |
   = note: expected type `&std::string::String`
   = note:    found type `&'static str`

error: aborting due to 2 previous errors

error: Could not compile `pastebin_rs`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment