[英]How can I capture whether an error occurred in Rust's swap_with_slice() vec method?
The documentation tells me that the swap_with_slice()
method can potentially panic if the slices are of two different lengths. 文档告诉我,如果切片具有两个不同的长度,那么
swap_with_slice()
方法可能会出现紧急情况。 I am writing a parser which deals with data coming across TCP. 我正在编写一个解析器,该解析器处理通过TCP传输的数据。 My encoding scheme is such that the following should never panic, unless it is a malformed request.
我的编码方案是这样的,下面不应该恐慌,除非它是一个畸形的请求。 In this case, I don't want my programme to crash, but instead handle the error.
在这种情况下,我不希望我的程序崩溃,而是处理错误。 How do I capture whether
swap_with_slice
panics or not? 如何捕获
swap_with_slice
是否swap_with_slice
紧急情况? Since it itself does not return a Result? 既然它本身不返回结果?
let mut bytes_id = [0; 16];
bytes_id.swap_with_slice(&mut self.raw_bytes[Headers::MessageLength as usize..Headers::Data as usize]);
这应该工作
let result = std::panic::catch_unwind(|| <expected_to_panic_operation_here>);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.