get()Available since: v3.0.0-alpha.1
The .get() method for the Value struct retrieves the value at a certain field for an object, or a certain index for an array. The method takes a &str or a usize as an argument.
NoteTo use this method, you must be using version
3.0.0-alpha.1or higher of SurrealDB when connecting to the Rust SDK.
Method Syntaxvalue.get(target)
use surrealdb::engine::any::connect; use surrealdb::Value; async fn main() -> surrealdb::Result<()> { let db = connect("mem://").await?; db.use_ns("test").use_db("test").await?; let mut res = db .query( "[ { a: { big: [ 'nested', 'object' ] } }, { another: { big: [ 'nested', 'object' ] } } ];", ) .await?; let as_value = res.take::<Value>(0)?; // Get the value at index 0, field 'a' // Output: { big: ['nested', 'object'] } println!("{}", as_value.get(0).get("a")); Ok(()) }
As the .get() method will always return a Value, internally a Value::None is returned when nothing is found at a certain index or field. The methods .is_none() and .into_option() can be used on the Value struct to check if the .get() method has returned a non-None value or not.