This "fixed" code is wrong. Instead of taking a reference to the borrow, it 
actually destructs it and copies the referenced Test. Since it's not keeping a 
reference anymore, that's why it works.

The issue with the original code is test.match_fn() borrow test, and Some(mut 
borrow_test) keeps that borrow alive in the borrow_test binding. Since it's 
used in the match arm, that means the borrow is alive, which prevents 
test.test_mutable() from being called.

I don't know how to fix this code when I have no idea what you're actually 
trying to accomplish here.

-Kevin

On Apr 25, 2014, at 12:55 AM, Philippe Delrieu <[email protected]> wrote:

> Thanks for your help. It works on the test but not in my main program.
> 
> I'll try to update the test to make it works like the main program but I have 
> not yet found what make the base code different.
> 
> Philippe
> 
> Le 24/04/2014 23:06, Artella Coding a écrit :
>> Hi, the following modified program seems to work (I am using rustc 
>> 0.11-pre-nightly (d35804e 2014-04-18 00:01:22 -0700) :
>> 
>> 
>> **********************************************************
>> use std::vec::Vec;
>> use std::rc::Rc;
>> use std::cell::RefCell;
>> 
>> struct Test;
>> 
>> impl Test {
>>      fn match_fn<'a>(&'a self) ->Option<&'a Test> {
>>          None
>>      }
>> 
>>      fn test_mutable<'a>(&'a mut self, test: &'a mut Test) {}
>> }
>> 
>> fn TestMatchBorrow()    {
>>      let mut viewList: Vec<~Test> = Vec::new();
>> 
>>      for ref mut test in viewList.mut_iter()    {
>>          match test.match_fn()   {
>>             Some(&mut borrow_test) => test.test_mutable(&mut borrow_test),
>>             None => {},
>>          }
>>      }
>> 
>> }
>> 
>> #[main]
>> fn main() {
>>      TestMatchBorrow();
>> }
>> **********************************************************
>> 
>> 
>> 
>> On Thu, Apr 24, 2014 at 9:23 PM, Philippe Delrieu <[email protected]> 
>> wrote:
>> Hello,
>> 
>> I have a problem in my code and I can't find a solution. I develop a test 
>> case that generate the same error. Any idea?
>> 
>> use std::vec::Vec;
>> use std::rc::Rc;
>> use std::cell::RefCell;
>> 
>> struct Test;
>> 
>> impl Test {
>>     fn match_fn<'a>(&'a self) ->Option<&'a Test> {
>>         None
>>     }
>> 
>>     fn test_mutable<'a>(&'a mut self, test: &'a Test) {}
>> }
>> 
>> fn TestMatchBorrow()    {
>>     let mut viewList: Vec<~Test> = Vec::new();
>> 
>>     for ref mut test in viewList.mut_iter()    {
>>         match test.match_fn()   {
>>             Some(mut borrow_test) => test.test_mutable(borrow_test),
>>             None => {},
>>         }
>>     }
>> 
>> }
>> 
>> #[main]
>> fn main() {
>>     TestMatchBorrow();
>> }
>> 
>> The test struct can't be changed.
>> If I don't put the borrow_test in test.test_mutable(borrow_test) it compile.
>> 
>> The error :
>> test_match.rs:22:38: 22:42 error: cannot borrow `***test` as mutable because 
>> it is also borrowed as immutable
>> test_match.rs:22             Some(mut borrow_test) => 
>> test.test_mutable(borrow_test),
>> ^~~~
>> test_match.rs:21:15: 21:19 note: previous borrow of `***test` occurs here; 
>> the immutable borrow prevents subsequent moves or mutable borrows of 
>> `***test` until the borrow ends
>> test_match.rs:21         match test.match_fn()   {
>> ^~~~
>> test_match.rs:24:10: 24:10 note: previous borrow ends here
>> test_match.rs:21         match test.match_fn()   {
>> test_match.rs:22             Some(mut borrow_test) => 
>> test.test_mutable(borrow_test),
>> test_match.rs:23             None => {},
>> test_match.rs:24         }
>> 
>> Philippe
>> _______________________________________________
>> Rust-dev mailing list
>> [email protected]
>> https://mail.mozilla.org/listinfo/rust-dev
>> 
> 
> _______________________________________________
> Rust-dev mailing list
> [email protected]
> https://mail.mozilla.org/listinfo/rust-dev

_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to