我正在为&[u8]
实现特征,但是我不能在特征实现中使用self
。我以为trait无法检测到类型,我应该使用where
子句,但是我不知道没有实现者怎么使用它。
use std::fmt::Debug;
pub trait Xor: Debug {
fn xor(&self,key_bytes: &[u8]) -> &[u8] {
for n in &self[..] {
dbg!(n);
}
unimplemented!()
}
}
impl Xor for [u8] {}
fn main() {
let xa = b"1234";
xa.xor(b"123");
}
error[E0608]: cannot index into a value of type `&Self`
--> src/main.rs:5:19
|
5 | for n in &self[..] {
| ^^^^^^^^