example from documentation does not compile

Aperta
#439 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

  • #444 di @veeceey — chiusa senza merge
  • #456 di @TrueFurina — chiusa senza merge

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
45/100
Tipo di issue
Documentazione
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
rust
Ambito
documentation

Direzione di ricerca

Inizia individuando l'esempio nella documentazione che contiene le definizioni di PublicError, ErrorRepr, Foo e throw_foo, quindi compilalo come mostrato. Conferma che l'esempio non produca più E0277 o E0308 e che il comportamento documentato di gestione degli errori rimanga intatto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

// Private and free to change across minor version of the crate.
#[derive(thiserror::Error, Debug)] // PublicError is public, but opaque and easy to keep compatible.
#[error(transparent)]
pub struct PublicError(#[from] ErrorRepr);

impl PublicError {
    // Accessors for anything we do want to expose publicly.
}

// Private and free to change across minor version of the crate.
#[derive(thiserror::Error, Debug)]
enum ErrorRepr {
    #[error("foo")]
    Foo(#[from] Foo),
}

#[derive(thiserror::Error, Debug)]
#[error("foo_text")]
struct Foo;

fn throw_foo() -> Result<(), Foo> {
    return Err(Foo);
}

fn main() -> Result<(), PublicError> {
    throw_foo()?;
    println!("Hello, world!");
}
error[E0277]: `?` couldn't convert the error to `PublicError`
  --> src/main.rs:26:16
   |
25 | fn main() -> Result<(), PublicError> {
   |              ----------------------- expected `PublicError` because of this
26 |     throw_foo()?;
   |     -----------^ the trait `From<Foo>` is not implemented for `PublicError`
   |     |
   |     this can't be annotated with `?` because it has type `Result<_, Foo>`
   |
note: `PublicError` needs to implement `From<Foo>`
  --> src/main.rs:4:1
   |
 4 | pub struct PublicError(#[from] ErrorRepr);
   | ^^^^^^^^^^^^^^^^^^^^^^
note: alternatively, `Foo` needs to implement `Into<PublicError>`
  --> src/main.rs:19:1
   |
19 | struct Foo;
   | ^^^^^^^^^^
   = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
   = help: the trait `From<Foo>` is not implemented for `PublicError`
           but trait `From<ErrorRepr>` is implemented for it
   = help: for that trait implementation, expected `ErrorRepr`, found `Foo`

error[E0308]: mismatched types
  --> src/main.rs:25:14
   |
25 | fn main() -> Result<(), PublicError> {
   |    ----      ^^^^^^^^^^^^^^^^^^^^^^^ expected `Result<(), PublicError>`, found `()`
   |    |
   |    implicitly returns `()` as its body has no tail or `return` expression
   |
   = note:   expected enum `Result<(), PublicError>`
           found unit type `()`

Some errors have detailed explanations: E0277, E0308.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `tetete` (bin "tetete") due to 2 previous errors
Lingua principale
Rust
Stelle
5.6k
Fork
220
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di dtolnay/thiserror

Tutte le issue di dtolnay/thiserror

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.