Skip to content

Instantly share code, notes, and snippets.

@jonvaldes
Last active February 2, 2016 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonvaldes/e4abcbb250bf14a3041f to your computer and use it in GitHub Desktop.
Save jonvaldes/e4abcbb250bf14a3041f to your computer and use it in GitHub Desktop.
Failing to implement IntoIterator because if lifetime issues
vec3.rs:46:6: 46:8 error: the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates [E0207]
vec3.rs:46 impl<'a> IntoIterator for V3 {
^~
vec3.rs:46:6: 46:8 help: run `rustc --explain E0207` to see a detailed explanation
error: aborting due to previous error
use std::ops::Index;
use std::iter::IntoIterator;
#[derive(Copy, Clone, Default)]
pub struct V3 {
pub x: f32,
pub y: f32,
pub z: f32,
}
impl Index<i32> for V3
{
type Output = f32;
fn index(&self, index: i32) -> &f32 {
match index {
0 => &self.x,
1 => &self.y,
2 => &self.z,
_ => panic!(),
}
}
}
struct V3Iter<'a> {
v: &'a V3,
i: i32,
}
impl<'a> Iterator for V3Iter<'a> {
type Item = f32;
fn next(&mut self) -> Option<f32> {
let result = if self.i < 3 {
Some(self.v[self.i])
} else {
None
};
self.i += 1;
result
}
}
impl <'a> IntoIterator for V3 {
type Item = f32;
type IntoIter = V3Iter<'a>;
fn into_iter(&'a self) -> Self::IntoIter {
V3Iter { v: &self, i: 0 }
}
}
fn main(){
let vec = V3{x:212.0, y: 3121.0, z:32432.0};
let iter = V3Iter{v:&vec, i:0};
for x in iter{
println!("- {}", x);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment