-
Notifications
You must be signed in to change notification settings - Fork 324
/
Copy pathfib.rs
37 lines (35 loc) · 992 Bytes
/
fib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use tide::Request;
fn fib(n: usize) -> usize {
if n == 0 || n == 1 {
n
} else {
fib(n - 1) + fib(n - 2)
}
}
async fn fibsum(req: Request<()>) -> tide::Result<String> {
use std::time::Instant;
let n: usize = req.param("n")?.parse().unwrap_or(0);
// Start a stopwatch
let start = Instant::now();
// Compute the nth number in the fibonacci sequence
let fib_n = fib(n);
// Stop the stopwatch
let duration = start.elapsed().as_secs();
// Return the answer
let res = format!(
"The fib of {} is {}.\nIt was computed in {} secs.\n",
n, fib_n, duration,
);
Ok(res)
}
// Example: HTTP GET to http://localhost:8080/fib/42
// $ curl "http://localhost:8080/fib/42"
// The fib of 42 is 267914296.
// It was computed in 2 secs.
#[async_std::main]
async fn main() -> Result<(), std::io::Error> {
let mut app = tide::new();
app.at("/fib/:n").get(fibsum);
app.listen("0.0.0.0:8080").await?;
Ok(())
}