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?