#where is this function defined ?

42 messages · Page 1 of 1 (latest)

glad crypt
#
fn main() {
    let name : Result<i8, i8> = Ok(1);

    println!("{}", name.is_ok())
}

is is_ok() defined in std ? or it is a rust built-n function ? i am confused works with the result type only but not found in std

abstract knot
glad crypt
#

i cannot send screenshots

abstract knot
#

What do you mean doesn't work?

glad crypt
#

i did :

std::result::Result::is_ok

#

but doesn't work

#
fn main() {
    let name : Result<i8, i8> = Ok(1);

    println!("{}", name.std::result::Result::is_ok)
}

abstract knot
#

you can't call methods like that

glad crypt
#

how should i call it ?

abstract knot
#

Like you already had

glad crypt
#

confusing

#

why cannot ?????????

abstract knot
#

what's wrong with the original code?

glad crypt
#

it is weird

#

defined in result but cannot use ????

abstract knot
#

name.is_ok() is using it

glad crypt
#

i wanna add std::result::Result

abstract knot
#

what do you mean by "add"

glad crypt
#

std::result::Result::is_ok

#

i wanna make it like this

abstract knot
#

why this instead of just name.is_ok()?

glad crypt
#

because std::result::Result makes more sense

#

name.is_ok doesn't make sense to me

abstract knot
#

If you use rust enough, you'll learn how to understand it very quickly.

glad crypt
#

i need answer

fringe salmon
#

You could do std::result::Result::is_ok(&name), but why? Why would ever do that?

glad crypt
#

awww

#

that's the answer

#

i am looking for

#

😀

glad crypt
#

@fringe salmon mate what about this

fn main() {
    let start : Result<i8, i8> = Ok(1);
    
    println!("{}", std::result::Result::is_ok_and(|a| a == 1))
}
#

???

#

how to refer to start ?

fringe salmon
#

Okay, so what you're trying to do is directly call a method as a function. In Rust, methods are functions that have their first parameter as a self. This allows you to omit having to add arguments directly, but automatically putting the callee into the self parameter, i.e:

fn main() {
    let start : Result<i8, i8> = Ok(1);
    
    println!("{}", start.is_ok_and(|a| a == 1))
}

Is exactly equal to:

fn main() {
    let start : Result<i8, i8> = Ok(1);
    
    println!("{}", std::result::Result::is_ok_and(start, |a| a == 1))
}

What you've done does not work because you are not supplying the self parameter, which is normally inserted by the compiler when called as a method.

glad crypt
#

thanks man you are awesome

#

mate just one more question and i will go

Ok : is a variant

is_ok : is a method

#

right ?

fringe salmon
#

yep

glad crypt
#

yahoo

errant salmon
#

As a rust beginner rust can be really confusing, but in the same time it's so amazing because it just makes sense