html tool

显示标签为“设计思想”的博文。显示所有博文
显示标签为“设计思想”的博文。显示所有博文

2020年10月15日星期四

Optiondiff T --rust

https://doc.rust-lang.org/book/ch06-01-defining-an-enum.html

 Intense! In effect, this error message means that Rust doesn’t understand how to add an i8 and an Option<i8>, because they’re different types. When we have a value of a type like i8 in Rust, the compiler will ensure that we always have a valid value. We can proceed confidently without having to check for null before using that value. Only when we have an Option<i8> (or whatever type of value we’re working with) do we have to worry about possibly not having a value, and the compiler will make sure we handle that case before using the value.


In other words, you have to convert an Option<T> to a T before you can perform T operations with it. Generally, this helps catch one of the most common issues with null: assuming that something isn’t null when it actually is.


Not having to worry about incorrectly assuming a not-null value helps you to be more confident in your code. In order to have a value that can possibly be null, you must explicitly opt in by making the type of that value Option<T>. Then, when you use that value, you are required to explicitly handle the case when the value is null. Everywhere that a value has a type that isn’t an Option<T>, you can safely assume that the value isn’t null. This was a deliberate design decision for Rust to limit null’s pervasiveness and increase the safety of Rust code.

[popexizhi: Option<T> = T or Null, 并且 Option<T> 不可以与<T> 做一个维度上的操作,eg: Option<T> + T , 要求将Opetion<T>转换为T,这个过程要求处理null的可能,这个过程给开发者一个强制思考如何处理null的过程。这个是,安全就是提前将问题暴露的过程]