Hi!
Suppose I am defining a struct such as:
use num::Num; // 0.4.0
#[derive(Copy, Clone)]
pub struct Vector<Ti: Num,Tj: Num,Tk:Num>
{
i: Ti,
j: Tj,
k: Tk,
}
Then I want to take there square root of a vector, such as in this following code:
// Implement funcitonalities
impl<T:Num> Vector<T,T,T> {
pub fn norm(self) -> T {
let val = self.i*self.i + self.j*self.j + self.k*self.k;
return val.sqrt();
}
}
This method (sqrt) does not exist and gives the warning:
error[E0599]: no method named `sqrt` found for type parameter `T` in the current scope
--> src/math_vectors.rs:37:20
|
37 | return val.sqrt();
| ^^^^ method not found in `T`
|
= help: items from traits can only be used if the type parameter is bounded by the trait
help: the following traits define an item `sqrt`, perhaps you need to restrict type parameter `T` with one of them:
|
34 | impl<T: Float + Num> Vector<T,T,T> {
| ~~~~~~~~~~
34 | impl<T: Real + Num> Vector<T,T,T> {
| ~~~~~~~~~
34 | impl<T: Roots + Num> Vector<T,T,T> {
I know that it will work with "Float", but I want to be able to input numbers without thinking about putting ".", or if there are floats or not.
I guess in reality if I get a case of Vector(1,2.0,3) I want it to default to a float behaviour, but then doing:
(val as Float).sqrt();
Gives the error:
error[E0038]: the trait `Float` cannot be made into an object
--> src/math_vectors.rs:37:24
|
37 | return (val as Float).sqrt();
| ^^^^^ `Float` cannot be made into an object
Can someone help?
Thanks!