代码:
		let mut message = core::str::from_utf8(b"").unwrap();
		let mut status = core::str::from_utf8(b"").unwrap();
		let mut results = Vec::new();
		let mut i = Vec::new();
		if let JsonValue::Object(obj) = json_val {
			obj
			.iter()
			.filter(|(k, _)| {
				let key: Vec<u8> = k.iter().map(|c| *c as u8).collect();
				KEY_MESSAGE.as_bytes().to_vec() == key
				|| KEY_STATUS.as_bytes().to_vec() == key
				|| KEY_RESULT.as_bytes().to_vec() == key
			})
			.for_each(|(k, v)| {
				let vec_of_u8s: Vec<u8> = k.iter().map(|c| *c as u8).collect();
				let json_key = core::str::from_utf8(&vec_of_u8s).unwrap();
				if json_key == KEY_MESSAGE {
					if let JsonValue::String(obj) = v {
						i = obj.iter().map(|c| *c as u8).collect::<Vec<u8>>().clone();
						message = core::str::from_utf8(&i).unwrap().clone();
					}
				} else if json_key == KEY_STATUS {
					if let JsonValue::String(obj) = v {
						i = obj.iter().map(|c| *c as u8).collect::<Vec<u8>>().clone();
						status = core::str::from_utf8(i.as_ref()).unwrap().clone();
					}
				} else if json_key == KEY_RESULT {
					if let JsonValue::Array(array) = v {
						results = array.to_vec();
					}
				}
			});
		}
报错如下:
    |
248 |         let mut message = core::str::from_utf8(b"").unwrap();
    |             ----------- borrowed data cannot be stored into here...
...
262 |             .for_each(|(k, v)| {
    |                       -------- ...because it cannot outlive this closure
...
269 |                         message = core::str::from_utf8(&i).unwrap().clone();
    |                                                        ^^ cannot be stored outside of its closure
error: aborting due to previous error
貌似是生命周期不一致?请问代码里给i赋值的话,该怎么改呢?
谢谢
	    
	    
		1
	    
	    
	    共 3 条评论, 1 页
	
	
    
评论区
写评论如果一定要这样写,用fold是不是更合适?
Box < String > 和 Box < str > 对以下内容的回复:
试试看改成String,或者Box,或者Box。我猜可能是str是存在栈上的问题。