According to my example, this function expects an optional Permission struct. If this struct exists, we can return true. But if not exists, we can return false.
Am I right about this?
So,
LEFT = if let Some(Permissions) RIGHT = permission
I donβt understand this question, could you elaborate what βTHIS_WILL_CHECKED_FROM_VARIABLEAβ and βVARIABLEAβ are supposed to mean?
I donβt know what you mean by βa bad codeβ. In order to understand what this code will do, you should first fix this warning it produces
warning: variable `Permissions` should have a snake case name
--> src/lib.rs:8:17
|
8 | if let Some(Permissions) = permission {
| ^^^^^^^^^^^ help: convert the identifier to snake case: `permissions`
|
= note: `#[warn(non_snake_case)]` on by default
So changing the variable name, the code becomes something like e.g.
warning: unused variable: `p`
--> src/lib.rs:8:17
|
8 | if let Some(p) = permission {
| ^ help: if this is intentional, prefix it with an underscore: `_p`
|
= note: `#[warn(unused_variables)]` on by default
which helps understanding whatβs going on:
The check
if let Some(p) = permission
will inspect the permission variable (the right hand side of the ==, which is of type Option<Permissions>, and match it against the pattern βSome(p)β. The p in this pattern is the name of a new variable that is introduced (locally) by the if let statement.
An Option<Permissions> value can either be None, or Some(Permissions { user_id: β¦, admin: β¦ }).
The idea behind Option is that it captures a similar idea like null in many other existing languages. The types can then be used to distinguish nullable vs. not nullable value, if you will; i.e. Option<Permissions> is nullable, and Permissions isnβt. The None value plays the role of null, and Some(β¦valueβ¦) is the non-null case, the Some(β¦) constructor wraps the contained value. This is a useful principle because it means that in order to access a value that is nullable, youβll have to unwrap this extra βlayerβ around it first, which means you need to explicitly to a null-check.
Anywaysβ¦ back to if let: The pattern Some(p) matches any Some(β¦) value, and it only doesnβt match None.
So running
if let Some(p) = permission {
β¦CODEβ¦
}
effectively distinguishes whether or not permission is None; only if it isnβtNone, i.e. it isSome(β¦valueβ¦), then the pattern Some(p) matches. This pattern matching will then introduce and bind the pattern variable p to the corresponding value wrapped in the Some. The variable p is available for the β¦CODEβ¦ inside of the block attached to the if let statement.
In this case, this block just does return true, so it doesnβt use the value p at all. Hence also the βunused variableβ warning I pointed out earlier. The code youβre implementing can be simplified by using the .is_some() method of Option, as follows
Iβm not quite following the βif this struct existsβ terminology, but if what youβre asking about matches my explanation above, then the answer might be yes.
Also note that (as the book somewhat explains, too, IIRC) if let is essentially just syntactic sugar for a match.
A statement
if let πππππππ = ππππππππππ {
πππππ_πππππ
}
is just a shorthand for
if let πππππππ = ππππππππππ {
πππππ_πππππ
} else {}
And a statement
if let πππππππ = ππππππππππ {
πππππ_πππππ
} else {
ππππππ_πππππ
}
Thank you so much, @steffahn, Actually, my all questions are the same in different ways
Now, I understand, the LEFT side contains a new variable. This variable only exists, if the right value is not a None value.
I meant by bad code it's really a bad code and may not explain what I'm trying to understand.
Iβm not quite following the βif this struct existsβ terminology, but if what youβre asking about matches my explanation above, then the answer might be yes.
This showed me that I misunderstood before you explain me
Some languages like C# didn't have a feature like that. C# also has this feature with different syntax.
Thank you so much. I'm so happy to understand this. Thanks for your help. I'll mark this as a solution.
Iβm not really familiar with C# at all. Googling for βpattern matchingβ gives an example like
int? maybe = 12;
if (maybe is int number)
{
Console.WriteLine($"The nullable int 'maybe' has the value {number}");
}
else
{
Console.WriteLine("The nullable int 'maybe' doesn't hold a value");
}
and the (moral) equivalent of this in Rust syntax would indeed involve if let and look like
let maybe: Option<i32> = Some(12);
if let Some(number) = maybe {
println!("The optional i32 'maybe' has the value {number}");
} else {
println!("The optional i32 'maybe' doesn't hold a value");
}
that's what's beautiful about Rust, enjoying the invigorating feeling of victory when one finally understands and starts using some new concept "like a king"...
(i'm getting many of those in the past weeks/months while trying to get better with this neat language with help both here and on Rust discord server)