To fetch Pokemon data from an online API in Rust and store it in an array, we can use the reqwest
crate for making HTTP requests and parsing the JSON response. Here's an example code block that fetches the first 10 Pokemon from the PokeAPI and stores their names in an array:
main.rs624 chars26 lines
In this example, we define a Pokemon
struct with only the name
field, which is the only data we're interested in. We also define a PokemonList
struct that contains a vector of Pokemon
structs.
We make an HTTP GET request to the PokeAPI URL that returns the first 10 Pokemon, and then deserialize the response into a PokemonList
struct using the json
method provided by reqwest.
Finally, we map the Pokemon
structs into a vector of strings containing only the Pokemon names, and print the resulting array.
gistlibby LogSnag