Explain why its failure

fn failed_borrow<'a>() {
let _x = 12;
let y: &'a i32 = &_x;

fn main() {

print_refs(&four, &nine);
failed_borrow();

}

Please follow our code formatting guidelines:

You can edit the post by pressing the pencil button below your post.

1 Like

This is most likely where your compilation error originates. You are borrowing a local variable, so the lifetime of the borrow can’t be longer than the duration of the function call of failed_borrow. However, lifetime parameters such as the <'a> parameter of failed_borrow are user-provided, they can be anything which gives freedom to the caller but constrains the callee. The only thing that the callee knows about such a lifetime is that it’s longer than the duration of the function call. So the explanation is: lifetime 'a can be (and in fact always is) longer than the duration of the failed_borrow call (e.g. it could even be 'static if you’re calling failed_borrow::<'static>() except that this kind of explicit passing of the parameter doesn’t actually work in this example), but the variable _x can only be borrowed for shorter than the duration of the failed_borrow call due to _x being a local variable in the body of failed_borrow.

You example looks fairly contrived, it’s not at all clear what you’re trying to achieve or why you’re trying to explicitly annotate the type of y including the lifetime. When creating a local variable, even when you’re explicit about the type, it’s almost always enough to leave lifetimes implicit, so writing

let y: &i32 = &_x;

is what you’d want to do, and at least for your concrete example this will also make the code compile.

Maybe you’re not trying achieve anything but just trying to understand when and why you’ll get borrow checking errors (in order to learn to understand Rust better) which is fine, and in that case I hope my above explanation helps you with the understanding. If this is some kind of reduced example for a real problem you had when trying to code something that works, feel free to provide more context and/or a more complete example.

Also, in general, next to the code formatting like @alice mentioned, you should also try to include some explanatory text in a post in this forum (i.e. not just putting the question in the title) and if you’re asking for help with a compiler error message, please be so kind to also provide the full compiler error message. The rust compiler is extremely helpful in general, and even if you don’t understand some detail of what it’s saying yourself yet, people in this forum who are trying to help you will benefit from getting the error message in detail. Furthermore it’s often very helpful to also provide a complete code example that we can test out outselves, i.e. an example without anything missing like the definition for print_refs, four, and nine in your code. You can also provide a link to code in the rust playground which helps us a lot in helping you. Example:

Hi I’m trying to understand this error message that Rust is giving me:

fn failed_borrow<'a>() {
    let _x = 12;
    let _y: &'a i32 = &_x;
}

fn main() {
    failed_borrow();
}
error[E0597]: `_x` does not live long enough
 --> src/main.rs:3:23
  |
1 | fn failed_borrow<'a>() {
  |                  -- lifetime `'a` defined here
2 |     let _x = 12;
3 |     let _y: &'a i32 = &_x;
  |             -------   ^^^ borrowed value does not live long enough
  |             |
  |             type annotation requires that `_x` is borrowed for `'a`
4 | }
  | - `_x` dropped here while still borrowed

Here’s the code in a playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=912d425db13537ba4a68bbc7a33fd250

1 Like

In this case, you probably should’ve mentioned that the example comes from the Rust By Example book

Explicit annotation - Rust By Example

It’s yet another piece of information to help people help you.

// A function which takes no arguments, but has a lifetime parameter `'a`.
fn failed_borrow<'a>() {
    let _x = 12;

    // ERROR: `_x` does not live long enough
    let y: &'a i32 = &_x;
    // Attempting to use the lifetime `'a` as an explicit type annotation 
    // inside the function will fail because the lifetime of `&_x` is shorter
    // than that of `y`. A short lifetime cannot be coerced into a longer one.
}

Since the code already contains an explanation you should also mention what exactly you do and don’t understand from the given explanation.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.