官术网_书友最值得收藏!

Values and primitive types

Constants that have been initialized have a value. Values exist in different types: 70 is an integer, 3.14 is a float, and Z and θ are the type of a character. Characters are Unicode values that take four bytes of memory each. Godzilla is a string of type &str (which is Unicode UTF8 by default), true and false are the type of Boolean values. Integers can be written in different formats:

  • Hexadecimal format with 0x, like 0x46 for 70.
  • Octal format with 0o, like 0o106 for 70.
  • Binary format with 0b, like 0b1000110.
  • Underscores can be used for readability, as in 1_000_000. Sometimes the compiler will urge you to indicate more explicitly the type of number with a suffix, for example (the number after u or i is the number of memory bits used, namely: 8, 16, 32, or 64).
  • The 10usize denotes an unsigned integer of machine word size (usize), which can be any of the following types: u8, u16, u32, u64.
  • The 10isize denotes a signed integer of machine word size (isize), which can be any of the following types: i8, i16, i32, i64
  • In the cases above on a 64-bit operating system usize is in fact u64, and isize is equivalent to i64.
  • The 3.14f32 denotes a 32-bit floating-point number.
  • The 3.14f64 denotes a 64-bit floating-point number.
  • The numeric types i32 and f64 are the defaults if no suffix is given, but in that case to differentiate between them you must end an f64 value with .0, like:
let e = 7.0; 

In general, indicating a specific type is recommended.

Rust is like any other C-like language when it comes to the different operators and their precedence. However, notice that Rust does not have increment (++) or decrement (--) operators. To compare two values for equality use == , and != to test if they are different.

There is even the empty value () of zero size, which is the only value of the so called unit type (). It is used to indicate the return value when an expression or a function returns nothing (no value), as is the case for a function that only prints to the console. () is not the equivalent of a null value in other languages; () is no value, whereas null is a value.

主站蜘蛛池模板: 金塔县| 阜阳市| 禹州市| 凉山| 洪洞县| 仪征市| 祁阳县| 临江市| 莎车县| 年辖:市辖区| 东阳市| 永春县| 来凤县| 长泰县| 陵水| 阳江市| 渑池县| 花莲县| 冷水江市| 河南省| 中宁县| 含山县| 盘山县| 乌海市| 保德县| 石首市| 庆阳市| 镇坪县| 尚志市| 光山县| 广安市| 涿鹿县| 扶余县| 泌阳县| 秦安县| 襄汾县| 昌黎县| 尚志市| 两当县| 从化市| 嘉兴市|