【Rust】スライス


fn main(){
    let s = String::from("ABCDEFGH");
    let s1 = &s[0..3];
    let s2 = &s[3..6];
    println!("{} {}", s1, s2);


}

c++でsliceはないので、関数で書いている場合

#include <iostream>
#include <vector>

template<typename T>
void print(std::vector<T> const &v)
{
    for (auto i: v) {
        std::cout << i << ' ';
    }
    std::cout << std::endl;
}

template<typename T>
std::vector<T> slice(std::vector<T> const &v, int m, int n)
{
    auto first = v.cbegin() + m;
    auto last = v.cbegin() + n + 1;

    std::vector<T> vec(first, last);
    return vec;
}

int main()
{
    std::vector<int> v = {1, 2, 3, 4, 2, 2, 4, 6, 5};

    int m = 4, n = 7;
    std::vector<int> sub_vec = slice(v, m, n);
    print(sub_vec);
    return 0;
}
fn print(v: Vec<i32>) {
    for &i in & v {
        print!("{} ", &i);
    }
    print!("\n");
}

fn slicing(v: Vec<i32>, m: i32, n: i32) -> Vec<i32>
{
    let vec = &v[m..n];
    return vec.to_vec();
}

fn main(){
    let mut vect = vec![1, 2, 3, 4, 2, 2, 4, 6, 5];
    let v = slicing(vect, 4, 7);
    print(v);
}

>> slice indices are of type `usize` or ranges of `usize`

これでなぜ引数i32がエラーになるか理解できん…