Trait extendr_api::iter::AsStrIter

source ·
pub trait AsStrIter:
    GetSexp
    + Types
    + Length
    + Attributes
    + Rinternals {
    // Provided method
    fn as_str_iter(&self) -> Option<StrIter> { ... }
}

Provided Methods§

source

fn as_str_iter(&self) -> Option<StrIter>

Get an iterator over a string vector. Returns None if the object is not a string vector but works for factors.

use extendr_api::prelude::*;

test! {
    let obj = Robj::from(vec!["a", "b", "c"]);
    assert_eq!(obj.as_str_iter().unwrap().collect::<Vec<_>>(), vec!["a", "b", "c"]);

    let factor = factor!(vec!["abcd", "def", "fg", "fg"]);
    assert_eq!(factor.levels().unwrap().collect::<Vec<_>>(), vec!["abcd", "def", "fg"]);
    assert_eq!(factor.as_integer_vector().unwrap(), vec![1, 2, 3, 3]);
    assert_eq!(factor.as_str_iter().unwrap().collect::<Vec<_>>(), vec!["abcd", "def", "fg", "fg"]);
    assert_eq!(factor.as_str_iter().unwrap().collect::<Vec<_>>(), vec!["abcd", "def", "fg", "fg"]);

    let obj = Robj::from(vec![Some("a"), Some("b"), None]);
    assert_eq!(obj.as_str_iter().unwrap().map(|s| s.is_na()).collect::<Vec<_>>(), vec![false, false, true]);

    let obj = Robj::from(vec!["a", "b", <&str>::na()]);
    assert_eq!(obj.as_str_iter().unwrap().map(|s| s.is_na()).collect::<Vec<_>>(), vec![false, false, true]);

    let obj = Robj::from(vec!["a", "b", "NA"]);
    assert_eq!(obj.as_str_iter().unwrap().map(|s| s.is_na()).collect::<Vec<_>>(), vec![false, false, false]);
}

Object Safety§

This trait is not object safe.

Implementors§