Comprehensive Rust(繁体中文)經常在程式庫中使用,目的是建立可實作 From的自訂錯誤型別。 • anyhow 經常由應用程式使用,目的是協助函式中的錯誤處理機制,包括為錯誤加上背景資訊。 use anyhow::{bail, Context, Result}; use std::fs; use std::io::Read; use thiserror::Error; struct EmptyUsernameError(String); = String::with_capacity(100); fs::File::open(path) .with_context(|| format!("Failed to open {path}"))? .read_to_string(&mut username) .context("Failed to read")?; if username.is_empty() { bail!(EmptyUsernameError(path 提供的功能似曾相識,因為該功能提供了與 Go 中的 (T, error) 類似的使用模式和人體工學。 • anyhow::Context 是針對標準 Result 和 Option 型別實作的特徵。如要啟用這些型別的 .context() 和.with_context(),就必須使用 anyhow::Context。 29.6 使用 Result 進行結構化錯誤處理 以下程式碼實作一個非常簡單的運算式語言剖析器, 0 码力 | 358 页 | 1.41 MB | 10 月前3
julia 1.10.10. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1439 88.2 Working with context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1442 88.3 HMAC functions . false: julia> if 1 println("true") end ERROR: TypeError: non-boolean (Int64) used in boolean context This error indicates that the conditional was of the wrong type: Int64 rather than the required conditional chain is an error: julia> 1 && true ERROR: TypeError: non-boolean (Int64) used in boolean context On the other hand, any type of expression can be used at the end of a conditional chain. It will0 码力 | 1692 页 | 6.34 MB | 3 月前3
Julia 1.10.9. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1439 88.2 Working with context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1442 88.3 HMAC functions . false: julia> if 1 println("true") end ERROR: TypeError: non-boolean (Int64) used in boolean context This error indicates that the conditional was of the wrong type: Int64 rather than the required conditional chain is an error: julia> 1 && true ERROR: TypeError: non-boolean (Int64) used in boolean context On the other hand, any type of expression can be used at the end of a conditional chain. It will0 码力 | 1692 页 | 6.34 MB | 3 月前3
Julia 1.11.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1681 90.2 Working with context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1685 90.3 HMAC functions . false: julia> if 1 println("true") end ERROR: TypeError: non-boolean (Int64) used in boolean context This error indicates that the conditional was of the wrong type: Int64 rather than the required conditional chain is an error: julia> 1 && true ERROR: TypeError: non-boolean (Int64) used in boolean context On the other hand, any type of expression can be used at the end of a conditional chain. It will0 码力 | 2007 页 | 6.73 MB | 3 月前3
Julia 1.11.5 Documentation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1681 90.2 Working with context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1685 90.3 HMAC functions . false: julia> if 1 println("true") end ERROR: TypeError: non-boolean (Int64) used in boolean context This error indicates that the conditional was of the wrong type: Int64 rather than the required conditional chain is an error: julia> 1 && true ERROR: TypeError: non-boolean (Int64) used in boolean context On the other hand, any type of expression can be used at the end of a conditional chain. It will0 码力 | 2007 页 | 6.73 MB | 3 月前3
Julia 1.11.6 Release Notes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1681 90.2 Working with context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1685 90.3 HMAC functions . false: julia> if 1 println("true") end ERROR: TypeError: non-boolean (Int64) used in boolean context This error indicates that the conditional was of the wrong type: Int64 rather than the required conditional chain is an error: julia> 1 && true ERROR: TypeError: non-boolean (Int64) used in boolean context On the other hand, any type of expression can be used at the end of a conditional chain. It will0 码力 | 2007 页 | 6.73 MB | 3 月前3
Julia 1.12.0 RC1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1743 92.2 Working with context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1748CONTENTS xii 92.3 HMAC functions false: julia> if 1 println("true") end ERROR: TypeError: non-boolean (Int64) used in boolean context This error indicates that the conditional was of the wrong type: Int64 rather than the required conditional chain is an error: julia> 1 && true ERROR: TypeError: non-boolean (Int64) used in boolean context On the other hand, any type of expression can be used at the end of a conditional chain. It will0 码力 | 2057 页 | 7.44 MB | 3 月前3
Julia 1.12.0 Beta4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1742 92.2 Working with context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1747CONTENTS xii 92.3 HMAC functions false: julia> if 1 println("true") end ERROR: TypeError: non-boolean (Int64) used in boolean context This error indicates that the conditional was of the wrong type: Int64 rather than the required conditional chain is an error: julia> 1 && true ERROR: TypeError: non-boolean (Int64) used in boolean context On the other hand, any type of expression can be used at the end of a conditional chain. It will0 码力 | 2057 页 | 7.44 MB | 3 月前3
Julia 1.12.0 Beta3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1742 92.2 Working with context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1747CONTENTS xii 92.3 HMAC functions false: julia> if 1 println("true") end ERROR: TypeError: non-boolean (Int64) used in boolean context This error indicates that the conditional was of the wrong type: Int64 rather than the required conditional chain is an error: julia> 1 && true ERROR: TypeError: non-boolean (Int64) used in boolean context On the other hand, any type of expression can be used at the end of a conditional chain. It will0 码力 | 2057 页 | 7.44 MB | 3 月前3
julia 1.13.0 DEV. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1745 92.2 Working with context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1750CONTENTS xii 92.3 HMAC functions false: julia> if 1 println("true") end ERROR: TypeError: non-boolean (Int64) used in boolean context This error indicates that the conditional was of the wrong type: Int64 rather than the required conditional chain is an error: julia> 1 && true ERROR: TypeError: non-boolean (Int64) used in boolean context On the other hand, any type of expression can be used at the end of a conditional chain. It will0 码力 | 2058 页 | 7.45 MB | 3 月前3
共 22 条
- 1
- 2
- 3













