我正在尝试在 Rust 中实现 BST(对于 Rust 的可爱介绍中的 HW3 ),并且我遇到了生命周期错误,以及如何约束与没有生命周期的类型相关的类型的生命周期。
#[derive(Debug)]
pub struct BST<T>
where T: Ord
{
root: Option<Box<Node<T>>>,
}
// A couple dozen lines of BST stuff
impl<'a, T> IntoIterator for BST<T>
where T: Ord
{
type Item = T;
type IntoIter = BSTIter<'a, T>; // <- my intuition is that I should
// be able to say "BSTIter lives as
// long as BST."
fn into_iter(&'a mut self) -> BSTIter<'a, T> {
BSTIter::new(&mut self)
}
}
pub struct …Run Code Online (Sandbox Code Playgroud)