diff --git a/arrow/src/array/array_struct.rs b/arrow/src/array/array_struct.rs index 0e7304e80a46..e51600ec07fc 100644 --- a/arrow/src/array/array_struct.rs +++ b/arrow/src/array/array_struct.rs @@ -30,6 +30,32 @@ use crate::{ /// A nested array type where each child (called *field*) is represented by a separate /// array. +/// # Example: Create an array from a vector of fields +/// +/// ``` +/// use std::sync::Arc; +/// use arrow::array::{Array, ArrayRef, BooleanArray, Int32Array, StructArray}; +/// use arrow::datatypes::{DataType, Field}; +/// +/// let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true])); +/// let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31])); +/// +/// let struct_array = StructArray::from(vec![ +/// ( +/// Field::new("b", DataType::Boolean, false), +/// boolean.clone() as ArrayRef, +/// ), +/// ( +/// Field::new("c", DataType::Int32, false), +/// int.clone() as ArrayRef, +/// ), +/// ]); +/// assert_eq!(struct_array.column(0).as_ref(), boolean.as_ref()); +/// assert_eq!(struct_array.column(1).as_ref(), int.as_ref()); +/// assert_eq!(4, struct_array.len()); +/// assert_eq!(0, struct_array.null_count()); +/// assert_eq!(0, struct_array.offset()); +/// ``` pub struct StructArray { data: ArrayData, pub(crate) boxed_fields: Vec,