我不完全理解生活時間,但我認爲b
的一生將在self
之前結束。價值不夠長
那麼,如何編輯這段代碼呢?做一些內存拷貝?如果我做了一個新實例,這個生命週期必須堅持這種情況。
pub struct Formater {
layout: &'static str,
}
impl Formater {
pub fn new(layout: &'static str) -> Formater {
let regex = Regex::new(r"%\{([a-z]+)(?::(.*?[^\\]))?\}").unwrap();
let b = regex.replace_all(layout, "{}");
return Formater {
layout: &b,
};
}
}
錯誤:
error: `b` does not live long enough
--> src/format.rs:16:22
|
16 | layout: &b,
| ^does not live long enough
17 | };
18 | }
| - borrowed value only lives until here
|
= note: borrowed value must be valid for the static lifetime...
你應該使'layout'成爲'String'而不是'&'static str'。 http://stackoverflow.com/questions/24158114/rust-string-versus-str – kennytm
謝謝,這可以幫助我做到這一點。但是,如果我想使用&str.how來做到這一點?防鏽不能直接編輯生命期嗎? –
@彭靈俊你爲什麼要用'&str'? – wimh