How to slove the problem?


#1

use std::path::Path;
use std::sync::Arc;

struct Context {
    root: String,
    index: String,
}

fn get(uri: &str, context: &Arc<Context>) {
    
    if is_path(uri) {
        let p=if uri != "/" {
             let mut root = context.root.clone();
             root.push_str(uri);
             Path::new(&root)
        }else{
        Path::new(&context.index)
        };
        send(p);
    }

}

fn is_path(p: &str) -> bool {
    true
}
fn send(p: &Path) {

    println!("{:?}", p);
}

fn main() {
    let context = Arc::new(Context {
        root: "/home".to_string(),
        index: "/home/index.html".to_string(),
    });

    get("/3", &context.clone())
}

Output:


<anon>:16:25: 16:29 error: `root` does not live long enough
<anon>:16              Path::new(&root)
                                  ^~~~
<anon>:19:11: 21:6 note: reference must be valid for the block suffix following statement 0 at 19:10...
<anon>:19         };
<anon>:20         send(p);
<anon>:21     }
<anon>:14:50: 17:10 note: ...but borrowed value is only valid for the block suffix following statement 0 at 14:49
<anon>:14              let mut root = context.root.clone();
<anon>:15              root.push_str(uri);
<anon>:16              Path::new(&root)
<anon>:17         }else{
error: aborting due to previous error

If move the ‘let mut root = context.root.clone();’ out of the if block:

 let mut root = context.root.clone();

        let p=if uri != "/" {
            
             root.push_str(uri);
             Path::new(&root)
        }else{
        Path::new(&context.index)
        };

It works now, but i dont want that binding something dont always need. how to properly do this?


#2

You can define root outside the branch, but initialize it inside the branch.


#3

:slightly_smiling:thanks.


#4

You can even do something like this:

let root = if condition {
    // ...
    var // notice the missing `;`
} else {
    Path::new(/* something */)
}

If that somehow helps you.


#5

:slightly_smiling: Thanks


#6

Have you tried using PathBuf?

I did try to build your example here, but I encountered the problem that if you push a string starting with '/' onto the PathBuf the whole path is set to that string.

Also, I don’t know what you are trying to do, but maybe the PathBuf struct can save you a lot of time.…


#8

You are very kind。en, in the sample i’m working (a simple local server) i have used the PathBuf instead of String.