Can't mention more than two people.
nerditation
The code with From instead of TryFrom was a copy error, I was trying things while writing the post and pasted the wrong one. But even with the TryFrom the error is similar and shows a new error.
alice that's I understand, thanks.
jumpnbrownweasel Ok, I understood that, but the alternatives are what? don't use usize? use T as type for the new function?
kpreid your answer help me with the doubt on why it was complaining on the MyStruct::new(10);
while the try_from is another part the code. I got really confused thinking that the function signature was wrong.
Now let's go to what still are not clear to me.
Another one is when I see this types of errors.
the following other types implement trait `From<T>`:
<i32 as From<bool>>
<i32 as From<i8>>
<i32 as From<i16>>
<i32 as From<u8>>
<i32 as From<u16>>
Does this means that I need to implement the trait for some types?
To comeback to another error, if I remove the where.
--impl<T> QuickFind<T> where T: From<usize> {
++impl<T> QuickFind<T> {
The error now is this.
the trait bound `T: TryFrom<usize>` is not satisfied
let value = T::try_from(i).unwrap();
^ the trait `From<usize>` is not implemented for `T`, which is required by `T: TryFrom<_>`
That's why I put the where T: From<usize>
earlier
And it suggests to use this.
impl<T: std::convert::From<usize>> QuickFind<T> {
And this don't work too.
But ok let's go for what's working.
--impl<T> QuickFind<T> {
++impl<T> QuickFind<T> where T: TryFrom<usize> {
And now we got this.
error[E0277]: `<T as TryFrom<usize>>::Error` doesn't implement `Debug`
let value = T::try_from(i).unwrap();
^^^^^^ `<T as TryFrom<usize>>::Error` cannot be formatted using `{:?}` because it doesn't implement `Debug`
help: the trait `Debug` is not implemented for `<T as TryFrom<usize>>::Error`
required by a bound in `Result::<T, E>::unwrap`
pub fn unwrap(self) -> T
------ required by a bound in this associated function
where
E: fmt::Debug,
^^^^^^^^^^ required by this bound in `Result::<T, E>::unwrap`
help: consider further restricting the associated type
fn new(size: usize) -> QuickFind<T> where <T as TryFrom<usize>>::Error: Debug{
+++++++++++++++++++++++++++++++++++++++++
And I don't understand almost nothing from this message.
Do I have to implement something in some type?
And why T
is being casted to TryFrom<usize>>::Error
And to finalize, that's my point, I kinda get by just coping things from help messages from terminal to my code.
I can proficiently code in Typescript and GO, but I don’t know why rust messes with my mind.
Even reading the rustc --explain E0277
Where it says:
You tried to use a type which doesn't implement some trait in a place which expected that trait.
...
In order to fix this error, verify that the type you're using does implement the trait.
So lets implement debug for MyStruct...
I tried and got this error
#[derive(Debug)]
^^^^^ conflicting implementation for `QuickFind<_>`
I forgot about the derive =D
I know that I'm having problem understanding Rust.
But I usually don't find material explaining those silly things that my mind get hung up.
For example, I don't want to use.
fn new(size: usize) -> QuickFind<T> where <T as TryFrom<usize>>::Error: Debug {
That's so ugly to me that I want to find another way to fix those errors messages without just copying from the terminal to my code without understanding.
That's why I'm asking for recommendations on materials to read.
I never found a place that explain this.
fn new(size: usize) -> QuickFind<T> where <T as TryFrom<usize>>::Error: Debug {
Well, sorry for the long reply, I usually never post on forums to not bother people, but I spent a lot of time with that simple code that I was reconsidering my life choices as a dev
.
Thanks all for the replies.
Any web courses, books or material recommendation will be very appreciated.
ps: It compiled \o/