pub trait StringArrayType<'a>: ArrayAccessor<Item = &'a str> + Sized {
// Required methods
fn iter(&self) -> ArrayIter<Self>;
fn is_ascii(&self) -> bool;
}
Expand description
Abstracts iteration over different types of string arrays.
The StringArrayType
trait helps write generic code for string functions that can work with
different types of string arrays.
Currently three types are supported:
It is inspired / copied from arrow-rs.
§Examples
Generic function that works for StringArray
, LargeStringArray
and StringViewArray
:
/// Combines string values for any StringArrayType type. It can be invoked on
/// and combination of `StringArray`, `LargeStringArray` or `StringViewArray`
fn combine_values<'a, S1, S2>(array1: S1, array2: S2) -> Vec<String>
where S1: StringArrayType<'a>, S2: StringArrayType<'a>
{
// iterate over the elements of the 2 arrays in parallel
array1
.iter()
.zip(array2.iter())
.map(|(s1, s2)| {
// if both values are non null, combine them
if let (Some(s1), Some(s2)) = (s1, s2) {
format!("{s1}{s2}")
} else {
"None".to_string()
}
})
.collect()
}
let string_array = StringArray::from(vec!["foo", "bar"]);
let large_string_array = LargeStringArray::from(vec!["foo2", "bar2"]);
let string_view_array = StringViewArray::from(vec!["foo3", "bar3"]);
// can invoke this function a string array and large string array
assert_eq!(
combine_values(&string_array, &large_string_array),
vec![String::from("foofoo2"), String::from("barbar2")]
);
// Can call the same function with string array and string view array
assert_eq!(
combine_values(&string_array, &string_view_array),
vec![String::from("foofoo3"), String::from("barbar3")]
);
Required Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.