Skip to content

Instantly share code, notes, and snippets.

@o0Ignition0o
Created February 15, 2020 13:04
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save o0Ignition0o/f7ea7192313b030514ba3281e3236e9b to your computer and use it in GitHub Desktop.
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