简体   繁体   English

如何捕获Rust的swap_with_slice()vec方法中是否发生错误?

[英]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.

相关问题 Rust Vec:如何返回错误结果<t,e> ?</t,e> - Rust Vec: How to return error with Result<T,E>? 如何在 Rust 中简化这个可怕的嵌套错误处理代码? - How can I simplify this horribly nested error handling code in Rust? 如何展平 Result 的迭代器<Vec<T> ,E&gt; 返回 Result 的迭代器<T,E>生锈? - How do I flatten an iterator of Result<Vec<T>,E> to return an iterator of Result<T,E> in rust? 如何在 rust 中使用 if else 方法出错? - How to get error using if else method in rust? 如果发生错误异常,我如何计算执行重试运算符的次数 - How can I count the number of time that was executed retry operator wether the exception of an error occurred 在 R 中调试意外错误——如何找到错误发生的位置? - Debugging unexpected errors in R — how can I find where the error occurred? 如何在Rust中将glob :: GlobError转换为io :: Error? - How do I turn a glob::GlobError into an io::Error in Rust? 如何在WCF中进行序列化期间拦截发生的异常? - How can I intercept an exception occurred during serialization in WCF? 如何使用 rust Try trait with Option NoneError? - How can I use rust Try trait with Option NoneError? 如何判断复制节点搜索是否失败,或者我的节点或图是否无效? - How can I tell whether a copy-node search failed, or whether my node or graph are invalid?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM