-
Couldn't load subscription status.
- Fork 58
-
pub fn addnum(a: &arrayfire::Array<arrayfire::FloatingPoint>
,b: &arrayfire::Array<arrayfire::FloatingPoint>) -> arrayfire::Array<arrayfire::FloatingPoint> {
a + b
}
I want to make a function that accepts the trait arrayfire::FloatingPoint and produces arrayfire::FloatingPoint. What modifications do I need to make it compile?
Beta Was this translation helpful? Give feedback.
All reactions
In that case, you should look at https://doc.rust-lang.org/rust-by-example/generics.html
fn foo<T>(arg: T) { ... } that is how you define a generic function. In this case, the generic type T is what reused for defining the generic type Array<> that is in turn used a arguments to the function.
Replies: 3 comments
-
@BA8F0D39 I am not sure I understand your use case, the trait you mentioned is more like a contract checking so that only acceptable Array objects(Array is generic struct) are passed to the relevant functions.
Is this what you are trying to achieve ?
pub fn addnum(a: &arrayfire::Array<T>,b: &arrayfire::Array<T>) -> arrayfire::Array<T> where T: FloatingPoint { a + b }
Beta Was this translation helpful? Give feedback.
All reactions
-
I am new to rust and the rust compiler says cannot find type T in this scope.
How would you define the trait T in this case to use arrayfire::FloatingPoint?
Beta Was this translation helpful? Give feedback.
All reactions
-
In that case, you should look at https://doc.rust-lang.org/rust-by-example/generics.html
fn foo<T>(arg: T) { ... } that is how you define a generic function. In this case, the generic type T is what reused for defining the generic type Array<> that is in turn used a arguments to the function.
Beta Was this translation helpful? Give feedback.