#why is this ?

48 messages · Page 1 of 1 (latest)

misty sandal
#

why static is not required in option but required in result ?

quiet field
#

Can you send some example code demonstrating your issue? As it is, it's very hard to figure out what you mean

misty sandal
quiet field
#

That's not helpful; I don't know what you mean by static, or required, or not required. If you could send an example of what you mean, I could try and help

misty sandal
#

what do you mean by what is static

quiet field
#

Do you mean a static variable? A static lifetime?

misty sandal
#
fn check(age : i8) -> Result<&str, &str> {
    if age > 18 {
        return Ok("ok : accepted");
    }

    else{
        return Err("no : denied");
    }
}

fn main() {
    let myage : i8 = 98;

    println!("{:?}", check(myage));
}
#

why static is needed in result here ?

quiet field
#

?play

fn check(age : i8) -> Result<&str, &str> {
    if age > 18 {
        return Ok("ok : accepted");
    }

    else{
        return Err("no : denied");
    }
}

fn main() {
    let myage : i8 = 98;

    println!("{:?}", check(myage));
}
wise sleetBOT
#
error[E0106]: missing lifetime specifiers
 --> src/main.rs:1:30
  |
1 | fn check(age : i8) -> Result<&str, &str> {
  |                              ^     ^ expected named lifetime parameter
  |                              |
  |                              expected named lifetime parameter
  |
  = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
help: consider using the `'static` lifetime
  |
1 | fn check(age : i8) -> Result<&'static str, &'static str> {
  |                               +++++++       +++++++

For more information about this error, try `rustc --explain E0106`.```
misty sandal
#

why static needed ?

quiet field
#

That's because Rust can't assume that the &str is 'static, so you have to tell it

#

A 'static lifetime in this case means that the &str is always valid to use

#

?play

fn check(age : i8) -> Option<&str> {
    if age > 18 {
        return Some("ok : accepted");
    }

    else{
        return None;
    }
}

fn main() {
    let myage : i8 = 98;

    println!("{:?}", check(myage));
}
wise sleetBOT
#
error[E0106]: missing lifetime specifier
 --> src/main.rs:1:30
  |
1 | fn check(age : i8) -> Option<&str> {
  |                              ^ expected named lifetime parameter
  |
  = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
help: consider using the `'static` lifetime
  |
1 | fn check(age : i8) -> Option<&'static str> {
  |                               +++++++

For more information about this error, try `rustc --explain E0106`.```
quiet field
#

See how it doesn't work with Option either?

misty sandal
#

it works in option

daring crystal
#

in general, references in function signature always need a lifetime specifier along with them. there are special cases where the compiler can automatically infer and insert these lifetimes though

misty sandal
#

without static

quiet field
quiet field
misty sandal
quiet field
#

I'm not new, I've been programming in it for a while now. Regardless, that's besides the point; can you show me the code that does work?

daring crystal
#

just provide the code that works

misty sandal
#

i don't understand you know lifetimes but you don't know static ?

#

weird

daring crystal
#

you aren’t making it desirable to help you. if you have code that works without a specified lifetime and you want to know why it works, we can’t tell you that if you don’t show the code

quiet field
#

sigh. I know what 'static is. I simply don't know what code you're using that is working without 'static.

#

Is it something like this?

fn foo(string: &str) -> Option<&str> {
  &string[1..]
}
opal jay
#

There are a large number of ways code can be different. Showing the exact code you're wondering about is an important part of asking questions that can be answered well.

quiet field
misty sandal
#

?eval

fn check(name : &str) -> Option<&str> {
    if name.len() > 1 {
        return Some(name);
    }

    else {
        return None;
    }
}

fn main() {
    let myname : &str = "hello world";

    println!("{:?}", check(myname));
}
wise sleetBOT
#
Some("hello world")```
misty sandal
#

see

#

this works without static

#

but if i use result it won't work

quiet field
#

Because Rust sees this:

fn check(name: &'a str) -> Option<&'a str>
misty sandal
#

why ?

quiet field
#

In the example that doesn't work, it's because Rust can't do 'a i8, so there's nothing for it to base the lifetimes of Result<&str, &str> on

quiet field
# misty sandal why ?

Because 99.9% of the time, that's what you mean, so it's handy to have it do that automatically

daring crystal
# misty sandal why ?

it’s safe to assume that if there is one input borrow and one output borrow, the output borrow is derived from the input borrow

#

?eval

fn foo(_: &str) -> Result<&str, &str> { todo!() }
wise sleetBOT
#
()```
daring crystal
#

In addition, if there is one input lifetime but multiple output lifetimes, the compiler will assign the lifetime of the input borrow to all output borrows

quiet field
#

I would like to point out that had you just given your code from the start, you would've gotten an answer quicker and easier, and without any trouble

vapid gust
grand portal