(I’m just starting off with rust, so please be patient)

Is there an idiomatic way of writing the following as a one-liner, somehow informing rustc that it should keep the PathBuf around?

// nevermind the fully-qualified names
// they are there to clarify the code
// (that's what I hope at least)

let dir: std::path::PathBuf = std::env::current_dir().unwrap();
let dir: &std::path::Path   = dir.as_path();

// this won't do:
// let dir = std::env::current_dir().unwrap().as_path();

I do understand why rust complains that “temporary value dropped while borrowed” (I mean, the message says it all), but, since I don’t really need the PathBuf for anything else, I was wondering if there’s an idiomatic to tell rust that it should extend its life until the end of the code block.

You are viewing a single thread.
View all comments
2 points

This is a limitation of the current borrow checker. I think there are some efforts going on to improve on this situation, but right now that’s what we have to work with.

The borrow checker can keep unnamed values around for a single expression, but not beyond that.

permalink
report
reply

Rust Programming

!rust@lemmy.ml

Create post

Community stats

  • 144

    Monthly active users

  • 103

    Posts

  • 202

    Comments

Community moderators