DEV Community

How to Fetch a Web API with Rust 🦀

Henry Boisdequin on January 18, 2021

In this tutorial, I will be teaching you how to fetch a Web API asynchronously in Rust. This tutorial is for developers who know the basics of Rust...
Collapse
 
muxcmux profile image
🥔

Nice writeup.

I don't see why you need async in your program - all you do is hit an api once, wait for the result and print it on screen. This program should work exactly the same without async/await.

A better example would be to pass multiple company names and fetch the data concurrently.

Also I don't think the ? operator is the same as unwrap. unwrap will panic the current thread if the result an Err, while ? will propagate it to the call site.

Collapse
 
hb profile image
Henry Boisdequin

It's true that async is not needed but I wanted to show how to use async/await in Rust.

A better example would be to pass multiple company names and fetch the data concurrently.

Great idea! I will be sure to implement that type of tutorial soon. Also, the ? operator is not the same as unwrap, you're right. Since this post is more for beginners and it wasn't focused on the ? operator, I just wanted them to be familiar with the syntax and not have to worry about what it does.

Thanks for your input.

Collapse
 
didibear profile image
Adrien Turiot

Hey, what do you think of using unwrap_or for the optional parameter like in this Playground example here ?

Collapse
 
hb profile image
Henry Boisdequin

Wow, that looks great. Thanks for the suggestion!

Collapse
 
iarmankarimi profile image
Arman karimi • Edited

reqwest::get() makes a new http client on each call. use one client and get() from that if you are making many requests.