I have been through the tutorials and have seen Some used (I may have missed the introduction to it, but I don't think so...), but I do not have a firm grasp of what it does. Would anyone mind a second to give me a couple examples of what it is telling the compiler to do with a given value? Some uses like an explanation of below?
An example from the doc:
let mut x = Some(2);
match x.as_mut() {
Some(v) => *v = 42,
None => {},
}
assert_eq!(x, Some(42));
Some(T) is one of the variants of Option (Option in std::option - Rust). That means, Option can take two forms, each of them called a variant.
Option encodes the (expected) presence or absence of a value. Some(T) is the variant that says "there is a value of type T", None says "there is no value". This is kind of similar to the concept of having null or a pointer to a value.
Rust doesn't allow you to have any arbitrary value being absent though, like for example Java does (anything can be null). Rust forces you to actually declare that (by using Option<T> instead of T and to unwrap that condition: you have to check for Option being Some(T) or None before working with the (possible) contents. Through that, it achieves safety and points where other languages would throw NullPointerException.
If the alternative of a value not being available is the result of an error and not expected in normal operation, you should use Result instead.
So Some(T) is saying give me the value T points to or null? Ok I think that is starting to click. Time to go play with it a bit see if I can cement the concept in place. Thank you for your response!
There are two different way you can use Some(x). One is in the line
let mut x = Some(2);
This constructs a value of type Option<int> [1]. You are creating a "box" around the integer. Why would you do that? Well, it allows you to have one more value than a simple integer: None. So you can pass optional values around without having to designate a perfectly fine integer value (like -1 in many cases in C) that is "special" and stands for "error" or "impossible".
The other use is in the line
match x.as_mut() {
Some(v) => *v = 42,
None => {}
This deconstructs the value again, giving you back the contents of the Some box -- but you have to handle the None case as well! (The as_mut() makes it a bit more complicated: it gives you not the contents of the original "box", but a mutable reference to it.)
So, in expressions Some() constructs a value, in patterns in deconstructs a value.
[1] int here can stand for different integer types, since you only use literals, but that doesn't matter here.
I think it clicks if you avoid saying null. It's similar, but not quite the same. null (for most) describes the actual use of a null pointer. None has no pointer semantics, but is just the case where in the case where there could be something or nothing, nothing is there.
(Haskell, by the way, calls that type Maybe, maybe that helps )
if I wanted to make a vec of pointers to stucts that all have impl certain_trait in common is there a strait forward way to do this? I keep running into lifetime or type mismatch errors...seems strait forward enough...