3 ms·
And what should be defined behavior? There are quite a few choices that depend on particular situations. Feature designers have no knowledge about what would yo
by FpUser 3y ago
And what should be defined behavior? There are quite a few choices that depend on particular situations. Feature designers have no knowledge about what would you want so they left it up to application programmer to check the situation upfront and act accordingly to their wishes. If you want same behavior across the whole application you can always write generic function doing just that.
- Ygg2 3y agoReturn either value or an EMPTY placeholder. Look Java did it. It returns a nullable value or Optional.
- FpUser 3y agoThen I have to check the result anyways. Same thing
- mughinn 3y agoYou should always check The argument is that Rust forces you and in C++ you can forget/the compiler can do what it wants
- Ygg2 3y agoNo. In Rust check is there by default, with optional unchecked access. In C++ the safety is off by default and you have to remember to check. It's like Yaml parsers, they had "load_yaml" which is unsafe and "safe_load_yaml" which is the secure option. Imagine no surprise when Rubyists went for shorter safe looking method and got their servers pwned.
- FpUser 3y agoI think you are wrong. I looked and in Rust doc it says that: "Removes the last element from a vector and returns it, or None if it is empty". You better be doing check for "None".
- LegionMammal978 3y agoThe check is still there, that's not under dispute. GP is saying that it's still not the "same thing" as C++, since Rust will refuse to compile the code unless you either perform the check, or actively opt into unchecked access. In contrast, C++ will happily compile the code if you unintentionally neglect to perform the check.
- FpUser 3y agoAre you saying that Rust will refuse to compile code that does not explicitly check option return result for empty?
- __jem 3y agoYes, you cannot use the result without matching on the result/option type.
- Ygg2 3y ago>Are you saying that Rust will refuse to compile code that does not explicitly Depends what you mean by explicit. Look at it like this: pub fn main() { let mut vec : Vec<i32> = vec![]; let x = vec.pop(); println!("{:?}",x); // prints: None } In this case you see the value is missing. vec.pop().expect("I want a value") will panic with "I want a value" because value is empty. And most rigorous way to deal with it is: if let Some(x) = vec.pop() { println!("{:?}",x); } else { println!("EMPTY"); // prints: Empty } will either print value or EMPTY depending on what the vector contains. Types like Result will issue warning that you didn't handle the cases.
- FpUser 3y agofor some reason I can not answer your subsequent reply so I do it here: I looked at your example and played a bit with it and yes I agree with you - compiler does help in this case. My old text: So instead of checking if vector is not empty you check that the return result is not empty. I do not see much difference. If Rust compiler would choke when "else" clause in your example is not present I would understand your point about compiler preventing improper access.
- deleted 3y ago[deleted]