Created
February 15, 2020 13:04
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn fib_nth(n: usize) -> usize { | |
if n == 0 || n == 1 { | |
n | |
} else { | |
fib_nth(n - 1) + fib_nth(n - 2) | |
} | |
} | |
async fn fibsum(req: tide::Request<()>) -> String { | |
use std::time::Instant; | |
let a: usize = req.param("a").unwrap_or(0); | |
let b: usize = req.param("b").unwrap_or(0); | |
// Start a stopwatch | |
let start = Instant::now(); | |
// Compute both fibs in sequence | |
let nth_a = fib_nth(a); | |
let nth_b = fib_nth(b); | |
let sum = nth_a + nth_b; | |
// Stop the stopwatch | |
let duration = Instant::now().duration_since(start).as_secs(); | |
// Return the answer | |
format!( | |
"The fib sum of {} and {} is {}.\nit was computed in {} secs.\n", | |
a, b, sum, duration, | |
) | |
} | |
fn main() -> Result<(), std::io::Error> { | |
use async_std::task; | |
task::block_on(async { | |
let mut app = tide::new(); | |
app.at("/fibsum/:a/:b").get(fibsum); | |
app.listen("127.0.0.1:8080").await?; | |
Ok(()) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment