Christophe,

Indeed, the idiomatic way is to use Result enum [1]. Note that you are
not limited to IoResult, you can use any custom error type, for
example, Result<T, &'static str>. The documentation (a link to which
is below) is very nice, it contains a lot of examples and use
patterns.

So, your constructor method will look like this:

    pub fn new() -> Result<Connection, SomeErrorType> { ... }

Usually library-specific Result<T, SomeErrorType> is abbreviated via a
type alias:

    type YourLibraryResult<T> = Result<T, SomeErrorType>;

IoResult is defined exactly like this:

    type IoResult<T> = Result<T, IoError>;

    [1]: http://static.rust-lang.org/doc/master/std/result/index.html

2014-05-10 21:38 GMT+04:00 Christophe Pedretti <[email protected]>:
> Hi all,
>
> thanks to Vladimir and Valerii for their previous detailed answer to
> my email, now i have a more general question.
>
> My Connection struct has a "new" method with open a connection to a
> database. This connection can fails. Three possiblities :
>
> 1/ "new" returns an IoResult<Connection>. The connection must be
> unwrapped from the IoResult before using it
> 2/ "new" returns a "Connection"; after the instantiation, a test must
> be made using a method like "getIoResult -> Option<IoResult>"
> 3/ any other way to do ????
>
> For me, the 1/ is the best one, the connection can not be used until
> it is returned by new. With 2/, the test can be forgotten.
>
> Any suggestion ?
>
> Thanks
>
> --
> Christophe
> http://chris-pe.github.io/Rustic
> _______________________________________________
> Rust-dev mailing list
> [email protected]
> https://mail.mozilla.org/listinfo/rust-dev
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to