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

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현