Advice for Rust library writers about Error

Posted 金庆

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Advice for Rust library writers about Error相关的知识,希望对你有一定的参考价值。

Advice for library writers

From: https://blog.burntsushi.net/rust-error-handling/

Idioms for Rust libraries are still forming, but if your library needs to report custom errors, then you should probably define your own error type. It’s up to you whether or not to expose its representation (like ErrorKind) or keep it hidden (like ParseIntError). Regardless of how you do it, it’s usually good practice to at least provide some information about the error beyond just its String representation. But certainly, this will vary depending on use cases.

At a minimum, you should probably implement the Error trait. This will give users of your library some minimum flexibility for composing errors. Implementing the Error trait also means that users are guaranteed the ability to obtain a string representation of an error (because it requires impls for both fmt::Debug and fmt::Display).

Beyond that, it can also be useful to provide implementations of From on your error types. This allows you (the library author) and your users to compose more detailed errors. For example, csv::Error provides From impls for both io::Error and byteorder::Error.

Finally, depending on your tastes, you may also want to define a Result type alias, particularly if your library defines a single error type. This is used in the standard library for io::Result and fmt::Result.

以上是关于Advice for Rust library writers about Error的主要内容,如果未能解决你的问题,请参考以下文章

Advice for students of machine learning--转

Advice for Middleware Vendors JNI中间件厂商开发建议

机器学习- 吴恩达Andrew Ng 编程作业技巧 for Week6 Advice for Applying Machine Learning

Coursera机器学习-第六周-Advice for Applying Machine Learning

#Week8 Advice for applying ML & ML System Design

斯坦福机器学习视频笔记 Week6 关于机器学习的建议 Advice for Applying Machine Learning