目标是拥有类似于(游乐场)的东西:
trait T {
fn get_mutable_attribute(&mut self) -> &mut String;
fn forwardable_append_attribute(mut self, new_value: &str) -> Self {
let attribute = self.get_mutable_attribute();
attribute.push_str(new_value);
println!("{}", attribute);
self
}
}
struct S {
attribute: String,
}
impl T for S {
fn get_mutable_attribute(&mut self) -> &mut String {
&mut self.attribute
}
}
fn main() {
let s = S {
attribute: "init".to_string(),
}
.forwardable_append_attribute("new_1")
.forwardable_append_attribute("new_2")
.forwardable_append_attribute("new_3");
}
这给出了错误:
error[E0277]: the size for values of type `Self` cannot be known at compilation time
--> src/main.rs:3:37
|
3 | fn forwardable_append_attribute(mut self, new_value: &str) -> Self {
| ^^^^^^^^ doesn't have a size known at compile-time
|
= help: unsized locals are gated as an unstable feature
help: consider further restricting `Self`
|
3 | fn forwardable_append_attribute(mut self, new_value: &str) -> Self where Self: std::marker::Sized {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: function arguments must have a statically known size, borrowed types always have a known size
|
3 | fn forwardable_append_attribute(&mut self, new_value: &str) -> Self {
| ^
另一种方法是为实现特征的每个对象定义特征方法,但这会在所有子对象(操场)之间引入重复:
trait T {
fn get_mutable_attribute(&mut self) -> &mut String;
fn forwardable_append_attribute(self, new_value: &str) -> Self;
}
struct S {
attribute: String,
}
impl T for S {
fn get_mutable_attribute(&mut self) -> &mut String {
&mut self.attribute
}
fn forwardable_append_attribute(mut self, new_value: &str) -> Self {
let attribute = self.get_mutable_attribute();
attribute.push_str(new_value);
println!("{}", attribute);
self
}
}
fn main() {
let s = S {
attribute: "init".to_string(),
}
.forwardable_append_attribute("new_1")
.forwardable_append_attribute("new_2")
.forwardable_append_attribute("new_3");
}