#why is this ?
48 messages · Page 1 of 1 (latest)
Can you send some example code demonstrating your issue? As it is, it's very hard to figure out what you mean
why static is not required in option but required in result ?
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
what do you mean by what is static
Do you mean a static variable? A static lifetime?
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 ?
?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));
}
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`.```
why static needed ?
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));
}
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`.```
See how it doesn't work with Option either?
it works in option
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
without static
Show me the code that works then
Because in this example, it doesn't work
are you new to rust ?
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?
this doesn’t help anyone answer your question.
just provide the code that works
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
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..]
}
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.
In this example, there's an argument that's a reference as well (string: &str), which means that Rust assumes that the return value (Option<&str>) has the same lifetime. It's like you wrote this:
fn foo(string: &'a str) -> Option<&'a str> {
&string[1..]
}
?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));
}
Some("hello world")```
Because Rust sees this:
fn check(name: &'a str) -> Option<&'a str>
why ?
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
Because 99.9% of the time, that's what you mean, so it's handy to have it do that automatically
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!() }
()```
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
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
@misty sandal Here are the rules on when you don't have to specify lifetimes https://doc.rust-lang.org/reference/lifetime-elision.html
@misty sandal this video might help your understanding of lifetimes.
https://youtu.be/gRAVZv7V91Q?si=OaaH0lGM3e03Yv0y
how i think about lifetimes.
hope it helps!
the article: https://smallcultfollowing.com/babysteps/blog/2018/04/27/an-alias-based-formulation-of-the-borrow-checker/
discord: https://discord.gg/X4znAuxJ