alamb opened a new issue #598:
URL: https://github.com/apache/arrow-rs/issues/598


   **Is your feature request related to a problem or challenge? Please describe 
what you are trying to do.**
   I am making constant arrays for tests from constant slices
   
   With 1.53 Rust introduces the ability to turn constant slices into 
iterators. For example. However, the collect for
   
   This does not work:
   ```rust
           let string_array: ArrayRef = Arc::new([Some("foo"), None, 
Some("bar")].into_iter().collect::<StringArray>());
   ```
   
   It produces this error:
   ```
   error[E0277]: a value of type `GenericStringArray<i32>` cannot be built from 
an iterator over elements of type `&Option<&str>`
     --> arrow_util/src/display.rs:43:92
      |
   43 |         let string_array: ArrayRef = Arc::new([Some("foo"), None, 
Some("bar")].into_iter().collect::<StringArray>());
      |                                                                         
                   ^^^^^^^ value of type `GenericStringArray<i32>` cannot be 
built from `std::iter::Iterator<Item=&Option<&str>>`
      |
      = help: the trait `FromIterator<&Option<&str>>` is not implemented for 
`GenericStringArray<i32>`
   ```
   
   But this does (for primitive arrays):
   ```rust
           let int64_array: ArrayRef = Arc::new([Some(-1), None, 
Some(2)].into_iter().collect::<Int64Array>());
   ```
   
   Instead, for strings we have to do
   ```rust
           let string_array: ArrayRef = Arc::new(vec![Some("foo"), None, 
Some("bar")].into_iter().collect::<StringArray>());
   ```
   
   
   **Describe the solution you'd like**
   I would like to be able to do (note the lack of `vec!`):
   
   ```rust
           let string_array: ArrayRef = Arc::new([Some("foo"), None, 
Some("bar")].into_iter().collect::<StringArray>());
   ```
   
   Perhaps by making the `FromIter` implementation fancier (right now it hard 
codes a `Option<&str>` but the iterator above tives a `&Option<&str>`:  
https://github.com/apache/arrow-rs/blob/master/arrow/src/array/array_string.rs#L255-L262
   
   **Additional context**
   
   Note -- the same sort if issue exists for `DictionaryArray` as well
   
   Does not work:
   ```rust
   let dict_array = [Some("a"), None, Some("b")]
                   .into_iter().collect::<DictionaryArray::<Int32Type>>();
   ```
   Does work
   ```rust
   let dict_array = vec![Some("a"), None, Some("b")]
                   .into_iter().collect::<DictionaryArray::<Int32Type>>();
   ```
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to