Created
February 15, 2020 13:04
-
-
Save o0Ignition0o/f7ea7192313b030514ba3281e3236e9b to your computer and use it in GitHub Desktop.
This file contains hidden or 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