Tag: 特征

generics结构的构造函数中的“期望types参数”错误

我正在尝试将活塞纹理存储在结构中。 struct TextureFactory<R> where R: gfx::Resources { block_textures: Vec<Rc<Texture<R>>>, } impl<R> TextureFactory<R> where R: gfx::Resources { fn new(window: PistonWindow) -> Self { let texture = Rc::new(gfx_texture::Texture::from_path( &mut *window.factory.borrow_mut(), "assets/element_red_square.png", Flip::None, &TextureSettings::new() ).unwrap()); let block_textures = Vec::new(); block_textures.push(texture); TextureFactory { block_textures: block_textures, } } } 这不会编译: src/main.rs:37:9: 39:10 error: mismatched types: expected `TextureFactory<R>`, found `TextureFactory<gfx_device_gl::Resources>` (expected […]

我如何实现我不拥有的types,我不拥有?

我想实现Vec的Shl特性,代码如下。 这会使vec << 4可能,这将是vec.push(4)好糖。 use std::ops::Shl; impl<T> Shl<T> for Vec<T> { type Output = Vec<T>; fn shl(&self, elem: &T) -> Vec<T> { self.push(*elem); *self } } fn main() { let v = vec![1, 2, 3]; v << 4; } 编译失败,出现以下错误: 不能提供一个扩展实现,在这个包中没有定义特征和types[E0117] 要么 types参数T必须用作某些本地types的types参数(例如MyStruct<T> ); 只有当前机箱中定义的特征才能用于types参数[E0210] 据我了解,我不得不修补stdlib,更具体的collections::vec箱子。 是否有另一种方法来更改此代码编译成功?