[−][src]Function urban_rs::fetch_random
pub async fn fetch_random<'_>(
client: &'_ Client
) -> Result<Vec<Definition>, UrbanError>
Fetch a list of random definitions trough a reqwest client.
Example
use tokio::runtime::Runtime; // A reqwest client is needed to use the urban API let client = reqwest::Client::new(); // The function is async. Thus it needs an executor to be ran from inside a non-async // function. if let Ok(random_defs) = Runtime::new() .expect("Failed to create tokio runtime") .block_on(urban_rs::fetch_random(&client)) { // the random_defs is a vector of definitions. If it has no length then there were no words // found if random_defs.is_empty() { println!("No words were found"); return; } let first_random = &random_defs[0]; println!("Word of the day: {}!\n{}", first_random.word(), first_random.definition()); } else { println!("An error has occured while fetching the definition"); }
This example prints a random word and its definition.
Errors
The error type of the result is UrbanError. Which is an enum of three types.
- ReqwestError
- SerdeError
- UnknownJsonError
ReqwestError
This error occurs when reqwest fails to fetch from the Urban API.
SerdeError
This error occurs when the json received is invalid.
UnknownJsonError
This error occurs when the json received is valid but does not have the expected structure.
Empty result
There is the case in which the vector returned is empty. In theory it would always be populated as there is no reason for Urban to not find any definitions to return. But you should always be safe with fetches trough the internet and check that the vector is not empty.