[Documentation] Add example of .take() for error handling to query method (#3610)
This commit is contained in:
parent
686a140d80
commit
d672b34d72
1 changed files with 19 additions and 0 deletions
|
@ -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(())
|
||||
/// # }
|
||||
|
|
Loading…
Reference in a new issue