I tried to implement a builder pattern in Rust as an exercise. Then, I wanted to implement struct Director
with generics which handle concrete Builder
s and calculates the distance between two points.
Please turn a blind eye to the unsafe code:
struct VecLengthDirector<T> {
builder: T,
}
impl<T> VecLengthDirector<T> {
fn construct(&self) -> f64 {
let s = self.builder.start.unwrap();
let e = self.builder.end.unwrap();
let mut sum: i32 = 0;
for i in 0..s.len() {
sum += (s[i] - e[i]).pow(2);
}
(sum as f64).sqrt()
}
}
trait AbstractVecLengthBuider<T> {
fn addStartPoint(&mut self, point: T);
fn addEndPoint(&mut self, point: T);
}
struct TwoDimVecLengthBuilder {
start: Option<[i32; 2]>,
end: Option<[i32; 2]>,
}
impl AbstractVecLengthBuider<[i32; 2]> for TwoDimVecLengthBuilder {
fn addStartPoint(&mut self, point: [i32; 2]) {
self.start = Some(point);
}
fn addEndPoint(&mut self, point: [i32; 2]) {
self.end = Some(point);
}
}
pub fn test() {
let mut veclen1 = VecLengthDirector {
builder: TwoDimVecLengthBuilder {
start: None,
end: None,
},
};
veclen1.builder.addStartPoint([0, 0]);
veclen1.builder.addEndPoint([1, 1]);
println!(
"Distance between [0, 0] and [1, 1] is: {}",
veclen1.construct()
);
}
After running this code, the following error is reported:
error[E0609]: no field `start` on type `T`
--> src/main.rs:7:30
|
7 | let s = self.builder.start.unwrap();
| ^^^^^
error[E0609]: no field `end` on type `T`
--> src/main.rs:8:30
|
8 | let e = self.builder.end.unwrap();
| ^^^
I researched the error code and read Rust's documentation, but I couldn't understand what is happening and what's wrong.