[Documentation] Add example of .take() for error handling to query method ()

This commit is contained in:
Dave MacLeod 2024-03-07 18:34:19 +09:00 committed by GitHub
parent 686a140d80
commit d672b34d72
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -636,6 +636,25 @@ where
///
/// // Get all of the results from the second query
/// let people: Vec<Person> = result.take(1)?;
///
/// #[derive(serde::Deserialize)]
/// struct Country {
/// name: String
/// }
///
/// // The .take() method can be used for error handling
///
/// // If the table has no defined schema, this query will
/// // create a `country` on the SurrealDB side, but...
/// let mut result = db
/// .query("CREATE country")
/// .await?;
///
/// // It won't deserialize into a Country struct
/// if let Err(e) = result.take::<Option<Country>>(0) {
/// println!("Failed to make a country: {e:#?}");
/// assert!(e.to_string().contains("missing field `name`"));
/// }
/// #
/// # Ok(())
/// # }