我正在学习Rust中的命名生命周期 ,我很难理解它们在实现一个特征时代表了什么.具体来说,我很难理解这段来自libserialize/hex.rs的代码.为了简洁起见,我删除了一些 comments .
pub trait ToHex {
fn to_hex(&self) -> ~str;
}
static CHARS: &'static[u8] = bytes!("0123456789abcdef");
impl<'a> ToHex for &'a [u8] {
fn to_hex(&self) -> ~str {
let mut v = slice::with_capacity(self.len() * 2);
for &byte in self.iter() {
v.push(CHARS[(byte >> 4) as uint]);
v.push(CHARS[(byte & 0xf) as uint]);
}
unsafe {
str::raw::from_utf8_owned(v)
}
}
}
我理解CHARS定义中的'static
生存期,但我对ToHex实现中定义的生存期感到困惑.What do named lifetimes represent in the implementation of a trait?