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`.