Skip to content

RefCell value lifetime check error #35008

Closed
@paomian

Description

@paomian

when I use RefCell like after code.

use std::cell::RefCell;

fn test() -> bool {
    let a = RefCell::new(5);
    *a.borrow_mut() < 6    //error
    //return *a.borrow_mut() < 6;   // ok
}
fn main() {
    test();
}

https://is.gd/T8Wmps
will trigger a compile error

error: `a` does not live long enough
 --> <anon>:5:6
  |
5 |     *a.borrow_mut() < 6    // error
  |      ^
  |
note: reference must be valid for the destruction scope surrounding block at 3:18...
 --> <anon>:3:19
  |
3 | fn test() -> bool {
  |                   ^
note: ...but borrowed value is only valid for the block suffix following statement 0 at 4:28
 --> <anon>:4:29
  |
4 |     let a = RefCell::new(5);
  |                             ^

error: aborting due to previous error
rustc --version --verbose
rustc 1.12.0-nightly (936bfea94 2016-07-20)
binary: rustc
commit-hash: 936bfea94b800551c972e5689ae7da86d3d601de
commit-date: 2016-07-20
host: x86_64-apple-darwin
release: 1.12.0-nightly

cargo --version --verbose
cargo 0.13.0-nightly (664125b 2016-07-19)

MacOS EI Capitan 10.11.6

Metadata

Metadata

Assignees

No one assigned

    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