I've got some code that looks somewhat like the following:
let incoming: Vec<u8> = Vec::new();
match some_function(|data| {
let temp = &mut incoming;
Ok(*temp.write(data).unwrap())
}) {
Ok(v) => v,
Err(err) => return false,
};
Now the error I'm getting is the following:
error: type `usize` cannot be dereferenced
--> src/stripe_connection.rs:127:16
|
127 | Ok(*temp.write(data).unwrap())
For some reason I can't get my head around what I'm doing wrong, as the borrowing/lifecycle is still very new to me.
usize
doesn't implement the Deref
trait, so you can't apply *
to it. write()
returns a Result<usize>
, so when you unwrap()
it and apply the dereferencing asterisk:
*temp.write(data).unwrap()
You are attempting to do
*usize
Which is not possible.
I think you may be getting the operator precedence wrong.
*temp.write(data).unwrap()
is equivalent to *(temp.write(data).unwrap())
, not to (*temp).write(data).unwrap()
. You could write the latter, but it's unnecessary, because the compiler will automatically dereference pointers in the subject of a method call (i.e. the x
in x.f(..)
). So you should just remove the *
altogether here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With