Skip to content

Instantly share code, notes, and snippets.

@o0Ignition0o

o0Ignition0o/main.rs

Created Feb 15, 2020
Embed
What would you like to do?
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
You can’t perform that action at this time.