struct test<'a> {
name: &'a str
}
impl<'a> test<'a> {
fn modify_name(&'a mut self) {
let new_name = self.name.replacen("az", "", 1);
// i want to add this name to self with the same lifetime as self
self.name = &'a new_name[..];
}
}
不允许像上面这样的生命周期,如果我没有指定str的生命周期,rust会在函数结束后删除它. 也不能解引用self.name,因为"str在编译时不能被知道.
我try 在网上查找,但只找到了使用字符串而不是&;str的建议.
我想知道是否可以修改引用的 struct 字段. (我在 struct 中使用了很多&;str,很少修改它们,所以我更喜欢使用&;str)
我不确定这是否可能.如果不是,请向我解释,我遗漏了什么?