1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#[cfg(test)]
mod tests {

    use super::*;
    #[test]
    fn clip_test() -> Result<(), &'static str> {
        let vector: Vec<u8> = vec![5,10,15,20,25,30,35,40,45];
        println!("{:?}", clip(vector,1, 3)?);
        Ok(())
    }
}

/// # Clip
///
/// Function to get a part of a vector
///
/// ## Examples
///
/// ```rs
/// let vector: Vec<u8> = vec![5,10,15,20,25,30,35,40,45];
/// let clipped: Vec<u8> = clip(vector,1, 3)?;
/// println!("{}", clipped[0]); // 10
/// ```
pub fn clip<'a, T: Copy>(vector: Vec<T>, start: usize, end: usize) -> Result<Vec<T>, &'a str>{
    if start < 0 {
        return Err("Starting index has to be 0 or more");
    }
    if end >= vector.len() {
        return Err("Ending index has to be less than vector length");
    }
    let mut clipped : Vec<T> = vec![];
    for i in start..end {
        clipped.push(vector[i]);
    }
    Ok(clipped)
}