Skip to content

appropriate lifetime and match #7502

Closed
Closed
@remifontan

Description

@remifontan

following code does not compile:

struct Vec { x:float, y:float, z:float }
impl Vec {
    pub fn getRef<'a>(&'a mut self, i:uint) -> &'a mut float {
        // if(i==0) { &mut self.x }
        // else if(i==1) { &mut self.x }
        // else {&mut self.x }

        match(i) {
            0 => &mut self.x,
            1 => &mut self.y,
            _ => &mut self.z
        }
    }
}

I get following errors:

rustc test.rs -o test-test --test
test.rs:122:17: 122:28 error: cannot infer an appropriate lifetime due to conflicting requirements
test.rs:122             _ => &mut self.z
                             ^~~~~~~~~~~
test.rs:121:17: 121:28 note: first, the lifetime must be contained by the expression at 121:17...
test.rs:121             1 => &mut self.y,
                             ^~~~~~~~~~~
test.rs:121:17: 121:28 note: ...due to the following expression
test.rs:121             1 => &mut self.y,
                             ^~~~~~~~~~~
test.rs:120:17: 120:28 note: but, the lifetime must also be contained by the expression at 120:17...
test.rs:120             0 => &mut self.x,
                             ^~~~~~~~~~~
test.rs:120:17: 120:28 note: ...due to the following expression
test.rs:120             0 => &mut self.x,
                             ^~~~~~~~~~~
error: aborting due to previous error
make: *** [test-test] Error 101

The commented version that is using if statements does compile correctly.

I haven't tried yet with very latest build, so I'm not sure whether the problem is still there with rust 0.7

cheers,

Rémi

Metadata

Metadata

Assignees

Labels

A-lifetimesArea: Lifetimes / regions

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions