Here's a function with generic types, it receive a T and right shit(>>) it. At first I wrote like this:
fn test<T>(v : T) -> T {
v >> 1
}
It is off cause not compiled, the T need to be restricted on some traits which has method to do the shifting, and the compiler helped me as blew:
error[E0369]: no implementation for `T >> {integer}`
--> src/main.rs:8:7
|
8 | v >> 1
| - ^^ - {integer}
| |
| T
|
help: consider restricting type parameter `T`
|
7 | fn test<T: std::ops::Shr<Output = {integer}>>(v : T) -> T {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
but when I do as the compiler said, another error occurred:
error: cannot constrain an associated constant to a value
--> src/main.rs:7:26
|
7 | fn test<T: std::ops::Shr<Output = {integer}>>(v : T) -> T {
| ------^^^---------
| | |
| | ...cannot be constrained to this value
| this associated constant...
I'm confused about some place:
-
What is a
{integer}
type? I'v never seen theinteger
sounded by curly brackets. -
Isn't
Output
a "associated type"? Why the compiler said it is a "associated constant"? (To me, associated constant is some sort likeconst NAN: Self;
) -
How to write to make my code works?