3 ms·
I don't really understand the complaints in the 'ergonomics' section, this code won't even compile: fn dot_product(a: &Vector3, b: &Vector3) -> float {
by leshow 7y ago
I don't really understand the complaints in the 'ergonomics' section, this code won't even compile:
fn dot_product(a: &Vector3, b: &Vector3) -> float {
a.x*b.x + a.y*b.y + a.z*b.z
}
fn do_math(p1: &Vector3, p2: &Vector3, d1: &Vector3, d2: &Vector3, s: f32, t: f32) -> f32 {
let a = p1 + &(&d1*s);
let b = p2 + &(&d2*t);
let result = dot_product(&(&b - &a), &(&b - &a));
}
Namely, how are you going to multiply a struct like d1 by an f32? Rust has deref coercions also, so there's plenty of times you don't even need to put a &.
Also, I think the idiomatic thing to do would be to implement 'Add' for Vector3.
- hcs 7y ago> Namely, how are you going to multiply a struct like d1 by an f32? https://en.wikipedia.org/wiki/Scalar_multiplication https://en.wikipedia.org/wiki/Scalar_multiplication Here's a Rust Playground with the vector math impls he's probably assuming (dummied): https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=066b625c9cdcb99091a1a8c608f20604 https://play.rust-lang.org/?version=stable&mode=debug&editio...
- leshow 7y agoOh yeah. It wasn't clear from the blog post that he had implemented some of the std::ops operators for his Vector3 struct. You can get rid of the superfluous & symbols by implementing for &Vector3 also, so again, the ergonomics section doesn't really resonate with me.