我一直在查看文档,到目前为止我还没有看到内置函数来安全地移动项目Vec.
Vec::get存在,但这只是借用.Vec::remove存在,虽然它确实移出了向量,但如果索引超出范围,它也会发生恐慌.所以,我有两个问题:
remove(&mut self, index: usize) -> T恐慌如果超出范围.恐慌的原因可能是什么?为什么不实现它remove(&mut self, index: usize) -> Option<T>?道歉标题道歉.
这是一些示例代码:
use std::marker::PhantomData;
pub trait Foo {
fn foo(&self);
}
pub trait Bar<A: Foo> {
fn bar(&self, a: A);
}
pub struct Test<A, B>
where A: Foo,
B: Bar<A>
{
_phantom_r: PhantomData<A>,
bars: Vec<B>,
}
impl<A, B> Test<A, B>
where A: Foo,
B: Bar<A>
{
pub fn new() -> Test<A, B> {
Test {
_phantom_r: PhantomData,
bars: Vec::new(),
}
}
pub fn add_bar(&mut self, b: B) {
self.bars.push(b);
}
}
fn main() {
let t = Test::new();
}
Run Code Online (Sandbox Code Playgroud)
( …