2014-12-22 09:04:23 -08:00
|
|
|
use std::ops::Index;
|
|
|
|
|
2014-07-14 19:43:21 +12:00
|
|
|
struct MyVec<T> {
|
|
|
|
data: Vec<T>,
|
|
|
|
}
|
|
|
|
|
2015-01-08 22:02:42 +11:00
|
|
|
impl<T> Index<usize> for MyVec<T> {
|
2015-01-03 10:40:36 -05:00
|
|
|
type Output = T;
|
|
|
|
|
2015-03-21 21:16:57 -04:00
|
|
|
fn index(&self, i: usize) -> &T {
|
2014-10-14 23:05:01 -07:00
|
|
|
&self.data[i]
|
2014-07-14 19:43:21 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 02:39:40 +02:00
|
|
|
|
|
|
|
|
2014-07-14 19:43:21 +12:00
|
|
|
fn main() {
|
2021-08-25 02:39:40 +02:00
|
|
|
let v = MyVec::<Box<_>> { data: vec![Box::new(1), Box::new(2), Box::new(3)] };
|
2014-07-14 19:43:21 +12:00
|
|
|
let good = &v[0]; // Shouldn't fail here
|
|
|
|
let bad = v[0];
|
2020-09-02 10:40:56 +03:00
|
|
|
//~^ ERROR cannot move out of index of `MyVec<Box<i32>>`
|
2014-07-14 19:43:21 +12:00
|
|
|
}
|