我正在使用serde和serde_json 1.0来解码base64字符串中的数据:
fn from_base64_str<T: Deserialize>(string: &str) -> T {
let slice = decode_config(string, URL_SAFE).unwrap();
serde_json::from_slice(&slice).unwrap()
}
Run Code Online (Sandbox Code Playgroud)
当我编译时,我得到了这个:
error[E0106]: missing lifetime specifier
--> src/main.rs:6:23
|
6 | fn from_base64_str<T: Deserialize>(string: &str) -> T {
| ^^^^^^^^^^^ expected lifetime parameter
Run Code Online (Sandbox Code Playgroud)
检查serde doc,Deserialize定义为:
pub trait Deserialize<'de>: Sized {
Run Code Online (Sandbox Code Playgroud)
所以我添加了生命周期:
fn from_base64_str<'de, T: Deserialize<'de>>(string: &str) -> T {
let slice = decode_config(string, URL_SAFE).unwrap();
serde_json::from_slice(&slice).unwrap()
}
Run Code Online (Sandbox Code Playgroud)
然后编译器告诉我:
error: `slice` does not live long enough
--> src/main.rs:11:29
|
11 | serde_json::from_slice(&slice).unwrap()
| ^^^^^ does …Run Code Online (Sandbox Code Playgroud) 最近,我试图解决Haskell 99问题,第66次(紧凑地布置树).我成功了,但对这里的解决方案感到困惑(http://www.haskell.org/haskellwiki/99_questions/Solutions/66).
layout :: Tree a -> Tree (a, Pos)
layout t = t'
where (l, t', r) = layoutAux x1 1 t
x1 = maximum l + 1
layoutAux :: Int -> Int -> Tree a -> ([Int], Tree (a, Pos), [Int])
layoutAux x y Empty = ([], Empty, [])
layoutAux x y (Branch a l r) = (ll', Branch (a, (x,y)) l' r', rr')
where (ll, l', lr) = layoutAux (x-sep) (y+1) l
(rl, r', rr) …Run Code Online (Sandbox Code Playgroud)