Why does Rust expect double borrow (`&&'a mut T`)E0308 mismatched types with simple generic functionHow to return an instance of a trait?Why does the Rust borrow checker ignore drop()?Error Reading & Calculating QuotientMultiple borrow in RustWhy does returning a &[u8] rather than u8 from a function in Rust borrow self?Why can the Rust compiler break borrowing rules when using Rust 1.31?How to fix “borrowed” issue in the RUST programming?Rust mut borrow fail in closure

Short story about a potato hotel that makes its guests into potatoes throughout the night

How important is knowledge of trig identities for use in Calculus

Does the US Armed Forces refuse to recruit anyone with an IQ less than 83?

When Vesuvan Shapeshifter copies turn face up replacement effects, why do they work?

What's the correct way to determine turn order in this situation?

French license plates

Quote to show students don't have to fear making mistakes

PhD Length: are shorter PhD degrees (from different countries) valued differently in other counter countries where PhD Is a longer process?

Drawing Maps; flat distortion

Could Boris Johnson face criminal charges for illegally proroguing Parliament?

Does it require less energy to reach the Sun from Pluto's orbit than from Earth's orbit?

Duck, duck, gone!

Realistically, how much do you need to start investing?

Why do popular TCP-using services have UDP as well as TCP entries in /etc/services?

Shell Sort, Insertion Sort, Bubble Sort, Selection Sort Algorithms (Python)

Why did they use ultrafast diodes in a 50 or 60 Hz bridge?

If I travelled back in time to invest in X company to make a fortune, roughly what is the probability that it would fail?

Why is there such a singular place for bird watching?

Is there anything on the ISS that would be destroyed if that object were returned to Earth?

Did the Soviet army intentionally send troops (e.g. penal battalions) running over minefields?

Sending mail to the Professor for PhD, after seeing his tweet

A word that refers to saying something in an attempt to anger or embarrass someone into doing something that they don’t want to do?

What is the difference between increasing volume and increasing gain?

Notation clarity question for a conglomerate of accidentals



Why does Rust expect double borrow (`&&'a mut T`)


E0308 mismatched types with simple generic functionHow to return an instance of a trait?Why does the Rust borrow checker ignore drop()?Error Reading & Calculating QuotientMultiple borrow in RustWhy does returning a &[u8] rather than u8 from a function in Rust borrow self?Why can the Rust compiler break borrowing rules when using Rust 1.31?How to fix “borrowed” issue in the RUST programming?Rust mut borrow fail in closure






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









0















My code looks like this:



pub enum Cache<'a, T> 
Pending(&'a dyn FnOnce() -> T),
Cached(T),


impl<'a, T> Cache<'a, T>
pub fn get(&self) -> &mut T
// This caches and borrows the T



impl<'a, T> PartialEq for Cache<'a, T>
where &'a mut T: PartialEq

fn eq(&self, other: &Self) -> bool
self.get().eq(other.get())




But implementing Eq fails with:



error[E0308]: mismatched types
--> src/lib.rs:20:23
|
20 | self.get().eq(other.get())
| ^^^^^^^^^^^ expected mutable reference, found type parameter
|
= note: expected type `&&'a mut T`
found type `&mut T`


I think I'm conceptually misunderstanding something.










share|improve this question


























  • Are you trying to compare the T objects themselves, or the references returned by .get()?

    – Frxstrem
    Mar 28 at 21:23











  • @Frxstrem Functionally, I want the T objects to be the same.

    – Mark
    Mar 28 at 21:29






  • 1





    &'a dyn FnOnce() -> T looks like a questionable type, since you can't ever call the function. (You need to own an FnOnce to be able to call it.)

    – Sven Marnach
    Mar 28 at 21:51

















0















My code looks like this:



pub enum Cache<'a, T> 
Pending(&'a dyn FnOnce() -> T),
Cached(T),


impl<'a, T> Cache<'a, T>
pub fn get(&self) -> &mut T
// This caches and borrows the T



impl<'a, T> PartialEq for Cache<'a, T>
where &'a mut T: PartialEq

fn eq(&self, other: &Self) -> bool
self.get().eq(other.get())




But implementing Eq fails with:



error[E0308]: mismatched types
--> src/lib.rs:20:23
|
20 | self.get().eq(other.get())
| ^^^^^^^^^^^ expected mutable reference, found type parameter
|
= note: expected type `&&'a mut T`
found type `&mut T`


I think I'm conceptually misunderstanding something.










share|improve this question


























  • Are you trying to compare the T objects themselves, or the references returned by .get()?

    – Frxstrem
    Mar 28 at 21:23











  • @Frxstrem Functionally, I want the T objects to be the same.

    – Mark
    Mar 28 at 21:29






  • 1





    &'a dyn FnOnce() -> T looks like a questionable type, since you can't ever call the function. (You need to own an FnOnce to be able to call it.)

    – Sven Marnach
    Mar 28 at 21:51













0












0








0








My code looks like this:



pub enum Cache<'a, T> 
Pending(&'a dyn FnOnce() -> T),
Cached(T),


impl<'a, T> Cache<'a, T>
pub fn get(&self) -> &mut T
// This caches and borrows the T



impl<'a, T> PartialEq for Cache<'a, T>
where &'a mut T: PartialEq

fn eq(&self, other: &Self) -> bool
self.get().eq(other.get())




But implementing Eq fails with:



error[E0308]: mismatched types
--> src/lib.rs:20:23
|
20 | self.get().eq(other.get())
| ^^^^^^^^^^^ expected mutable reference, found type parameter
|
= note: expected type `&&'a mut T`
found type `&mut T`


I think I'm conceptually misunderstanding something.










share|improve this question
















My code looks like this:



pub enum Cache<'a, T> 
Pending(&'a dyn FnOnce() -> T),
Cached(T),


impl<'a, T> Cache<'a, T>
pub fn get(&self) -> &mut T
// This caches and borrows the T



impl<'a, T> PartialEq for Cache<'a, T>
where &'a mut T: PartialEq

fn eq(&self, other: &Self) -> bool
self.get().eq(other.get())




But implementing Eq fails with:



error[E0308]: mismatched types
--> src/lib.rs:20:23
|
20 | self.get().eq(other.get())
| ^^^^^^^^^^^ expected mutable reference, found type parameter
|
= note: expected type `&&'a mut T`
found type `&mut T`


I think I'm conceptually misunderstanding something.







types rust borrow-checker borrowing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 29 at 7:11









hellow

7,1165 gold badges27 silver badges48 bronze badges




7,1165 gold badges27 silver badges48 bronze badges










asked Mar 28 at 21:10









MarkMark

10.2k5 gold badges69 silver badges98 bronze badges




10.2k5 gold badges69 silver badges98 bronze badges















  • Are you trying to compare the T objects themselves, or the references returned by .get()?

    – Frxstrem
    Mar 28 at 21:23











  • @Frxstrem Functionally, I want the T objects to be the same.

    – Mark
    Mar 28 at 21:29






  • 1





    &'a dyn FnOnce() -> T looks like a questionable type, since you can't ever call the function. (You need to own an FnOnce to be able to call it.)

    – Sven Marnach
    Mar 28 at 21:51

















  • Are you trying to compare the T objects themselves, or the references returned by .get()?

    – Frxstrem
    Mar 28 at 21:23











  • @Frxstrem Functionally, I want the T objects to be the same.

    – Mark
    Mar 28 at 21:29






  • 1





    &'a dyn FnOnce() -> T looks like a questionable type, since you can't ever call the function. (You need to own an FnOnce to be able to call it.)

    – Sven Marnach
    Mar 28 at 21:51
















Are you trying to compare the T objects themselves, or the references returned by .get()?

– Frxstrem
Mar 28 at 21:23





Are you trying to compare the T objects themselves, or the references returned by .get()?

– Frxstrem
Mar 28 at 21:23













@Frxstrem Functionally, I want the T objects to be the same.

– Mark
Mar 28 at 21:29





@Frxstrem Functionally, I want the T objects to be the same.

– Mark
Mar 28 at 21:29




1




1





&'a dyn FnOnce() -> T looks like a questionable type, since you can't ever call the function. (You need to own an FnOnce to be able to call it.)

– Sven Marnach
Mar 28 at 21:51





&'a dyn FnOnce() -> T looks like a questionable type, since you can't ever call the function. (You need to own an FnOnce to be able to call it.)

– Sven Marnach
Mar 28 at 21:51












1 Answer
1






active

oldest

votes


















4
















You can understand why Rust is expecting an &&mut T by looking at the definition of the eq() method in the PartialEq trait:



fn eq(&self, other: &Rhs) -> bool;


The types of the parameters to this method are &Self and &Rhs; since Rhs defaults to Self and you did not specify anything else in your trait bound, both arguments are expected to be of type &Self.



Now what is Self in this case? Your trait bound is this:



&'a mut T: PartialEq


So the only PartialEq implementation the compiler can use is the one for the type &'a mut T, so this is what Self is; &Self, in turn, must be &&'a mut T, which is exactly what the compiler is expecting.



You probably want the trait bound for T instead:



impl<'a, T> PartialEq for Cache<'a, T>
where
T: PartialEq,

fn eq(&self, other: &Self) -> bool
self.get() == other.get()




Also note that you can simply use == instead of explicitly calling eq(). It makes getting the types right a bit easier, since the compiler will take references of the arguments implicitly – a == b expands to PartialEq::eq(&a, &b).






share|improve this answer

























  • I had added &'a mut based on a compiler hint I got when I tried self.get().eq(other.get()). But now I see I should have used &other instead (or easier, ==).

    – Mark
    Mar 29 at 20:54












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);














draft saved

draft discarded
















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55406890%2fwhy-does-rust-expect-double-borrow-a-mut-t%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









4
















You can understand why Rust is expecting an &&mut T by looking at the definition of the eq() method in the PartialEq trait:



fn eq(&self, other: &Rhs) -> bool;


The types of the parameters to this method are &Self and &Rhs; since Rhs defaults to Self and you did not specify anything else in your trait bound, both arguments are expected to be of type &Self.



Now what is Self in this case? Your trait bound is this:



&'a mut T: PartialEq


So the only PartialEq implementation the compiler can use is the one for the type &'a mut T, so this is what Self is; &Self, in turn, must be &&'a mut T, which is exactly what the compiler is expecting.



You probably want the trait bound for T instead:



impl<'a, T> PartialEq for Cache<'a, T>
where
T: PartialEq,

fn eq(&self, other: &Self) -> bool
self.get() == other.get()




Also note that you can simply use == instead of explicitly calling eq(). It makes getting the types right a bit easier, since the compiler will take references of the arguments implicitly – a == b expands to PartialEq::eq(&a, &b).






share|improve this answer

























  • I had added &'a mut based on a compiler hint I got when I tried self.get().eq(other.get()). But now I see I should have used &other instead (or easier, ==).

    – Mark
    Mar 29 at 20:54















4
















You can understand why Rust is expecting an &&mut T by looking at the definition of the eq() method in the PartialEq trait:



fn eq(&self, other: &Rhs) -> bool;


The types of the parameters to this method are &Self and &Rhs; since Rhs defaults to Self and you did not specify anything else in your trait bound, both arguments are expected to be of type &Self.



Now what is Self in this case? Your trait bound is this:



&'a mut T: PartialEq


So the only PartialEq implementation the compiler can use is the one for the type &'a mut T, so this is what Self is; &Self, in turn, must be &&'a mut T, which is exactly what the compiler is expecting.



You probably want the trait bound for T instead:



impl<'a, T> PartialEq for Cache<'a, T>
where
T: PartialEq,

fn eq(&self, other: &Self) -> bool
self.get() == other.get()




Also note that you can simply use == instead of explicitly calling eq(). It makes getting the types right a bit easier, since the compiler will take references of the arguments implicitly – a == b expands to PartialEq::eq(&a, &b).






share|improve this answer

























  • I had added &'a mut based on a compiler hint I got when I tried self.get().eq(other.get()). But now I see I should have used &other instead (or easier, ==).

    – Mark
    Mar 29 at 20:54













4














4










4









You can understand why Rust is expecting an &&mut T by looking at the definition of the eq() method in the PartialEq trait:



fn eq(&self, other: &Rhs) -> bool;


The types of the parameters to this method are &Self and &Rhs; since Rhs defaults to Self and you did not specify anything else in your trait bound, both arguments are expected to be of type &Self.



Now what is Self in this case? Your trait bound is this:



&'a mut T: PartialEq


So the only PartialEq implementation the compiler can use is the one for the type &'a mut T, so this is what Self is; &Self, in turn, must be &&'a mut T, which is exactly what the compiler is expecting.



You probably want the trait bound for T instead:



impl<'a, T> PartialEq for Cache<'a, T>
where
T: PartialEq,

fn eq(&self, other: &Self) -> bool
self.get() == other.get()




Also note that you can simply use == instead of explicitly calling eq(). It makes getting the types right a bit easier, since the compiler will take references of the arguments implicitly – a == b expands to PartialEq::eq(&a, &b).






share|improve this answer













You can understand why Rust is expecting an &&mut T by looking at the definition of the eq() method in the PartialEq trait:



fn eq(&self, other: &Rhs) -> bool;


The types of the parameters to this method are &Self and &Rhs; since Rhs defaults to Self and you did not specify anything else in your trait bound, both arguments are expected to be of type &Self.



Now what is Self in this case? Your trait bound is this:



&'a mut T: PartialEq


So the only PartialEq implementation the compiler can use is the one for the type &'a mut T, so this is what Self is; &Self, in turn, must be &&'a mut T, which is exactly what the compiler is expecting.



You probably want the trait bound for T instead:



impl<'a, T> PartialEq for Cache<'a, T>
where
T: PartialEq,

fn eq(&self, other: &Self) -> bool
self.get() == other.get()




Also note that you can simply use == instead of explicitly calling eq(). It makes getting the types right a bit easier, since the compiler will take references of the arguments implicitly – a == b expands to PartialEq::eq(&a, &b).







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 28 at 22:09









Sven MarnachSven Marnach

385k85 gold badges777 silver badges716 bronze badges




385k85 gold badges777 silver badges716 bronze badges















  • I had added &'a mut based on a compiler hint I got when I tried self.get().eq(other.get()). But now I see I should have used &other instead (or easier, ==).

    – Mark
    Mar 29 at 20:54

















  • I had added &'a mut based on a compiler hint I got when I tried self.get().eq(other.get()). But now I see I should have used &other instead (or easier, ==).

    – Mark
    Mar 29 at 20:54
















I had added &'a mut based on a compiler hint I got when I tried self.get().eq(other.get()). But now I see I should have used &other instead (or easier, ==).

– Mark
Mar 29 at 20:54





I had added &'a mut based on a compiler hint I got when I tried self.get().eq(other.get()). But now I see I should have used &other instead (or easier, ==).

– Mark
Mar 29 at 20:54




















draft saved

draft discarded















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid …


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55406890%2fwhy-does-rust-expect-double-borrow-a-mut-t%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?