Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to convert from Result<T,E> to Option<T> #10789

Closed
lbonn opened this issue Dec 3, 2013 · 3 comments
Closed

Unable to convert from Result<T,E> to Option<T> #10789

lbonn opened this issue Dec 3, 2013 · 3 comments

Comments

@lbonn
Copy link
Contributor

lbonn commented Dec 3, 2013

I tried to use the existing conversion provided by the ToOption<T> trait which Result<T,E> implements:

#[deriving(Clone)]
struct A;

fn main() {
    let a : Result<A,int> = Ok(A);
    a.to_option();
}

But apparently, the compiler does not find its implementation:

option.rs:6:4: 6:18 error: type `std::result::Result<A,int>` does not implement any method in scope named `to_option`
option.rs:6     a.to_option();
                ^~~~~~~~~~~~~~
error: aborting due to previous error

Maybe I've done something wrong but I'm rather dry on ideas right now...

@sfackler
Copy link
Member

sfackler commented Dec 4, 2013

You need to add a use std::option::ToOption; to the top of the file. Methods defined by a trait are only callable if they're imported (some traits are implicitly imported in the prelude).

@alexcrichton
Copy link
Member

@sfackler is correct, this is working as intended (plus his explanation is spot on)

@lbonn
Copy link
Contributor Author

lbonn commented Dec 5, 2013

Ok I feel a bit dumb :), a re-read of the doc on traits and modules may be worth it.

On the other hand, it may be nice to have a visual help in the libstd docs to immediately see if a type/trait is re-exported by the prelude (or by another module maybe?). I'll try to look into rustdoc to see if this is doable.

flip1995 pushed a commit to flip1995/rust that referenced this issue May 20, 2023
fix example heading in `string_slice`

changelog: none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants