9.1 Numeric Vecor

It is recommanded to use Rust type, and then Rust compiler can optimize your code as much as possible.

When using R Object in Rust, you sometimes should care more about the safety. Move semantics from C api to Rust is mental strain. Make sure you pre-allocate R Object with enough space for more performance.

But, nothing is true forever, so let’s have some fun.

// #[rustr_export]
pub fn sum_numvec(x:NumVec) -> f64{
    let mut res = 0.0;
    for z in x.into_iter(){
        res += z;    
    }
    res
}
// #[rustr_export]
pub fn set_numvec(x:NumVec, ind: usize, v:f64)->RResult<NumVec>{
    let mut z = x;
    try!(z.set(ind,v));
    Ok(z)
}
rust(code = '
// #[rustr_export]
pub fn sum_numvec(x:NumVec) -> f64{
    let mut res = 0.0;
    for z in x.into_iter(){
        res += z;    
    }
    res
}
// #[rustr_export]
pub fn set_numvec(x:NumVec, ind: usize, v:f64)->RResult<NumVec>{
    let mut z = x;
    try!(z.set(ind,v));
    Ok(z)
}
')

sum_numvec(c(1,2,3,4,5))
#> [1] 15

set_numvec(c(1,2,3),0L,1)
#> [1] 1 2 3

set_numvec(c(1,2,3),4L,1)
#> Error in eval(expr, envir, enclos): rustr error: index out of bound

set_numvec(c(1,2,3),3L,1)
#> Error in eval(expr, envir, enclos): rustr error: index out of bound

set_numvec(c(1,2,3),2L,1)
#> [1] 1 2 1

Also you can read the docs http://docs.rustr.org/rustr/vector/numvec/struct.NumVecM.html .