2.2 Rust 数据类型
? ?2.2?数据类型let guess: u32 = "42".parse().expect("Not a number!"); Rust has four primary scalar types: integers,floating-point numbers,Booleans,and characters.? ?整数类型? ?u32,this type declaration indicates that the value it’s associated with should be an unsigned integer (signed integer types start with? Additionally,the? Note that all number literals except the byte literal allow a type suffix,such as? 整数溢出Let’s say that you have a? In release builds,Rust does not check for overflow,and instead will do something called “two’s complement wrapping.” In short,? ? 浮点类型Rust’s floating-point types are? fn main() {
let x = 64.0 ; //f64
let y: f32 = 32.0; //f32 println!("64:{},32:{}",x,y); }
The? ? 数字运算fn main() {
// addition
let _sum = 5 + 10; // subtraction let _difference = 95.5 - 4.3; // multiplication let _product = 4 * 30; // division let _quotient = 56.7 / 32.2; //1.7608695652173911 小数点后16位 let _ff32 = 7f32 / 3f32; //2.3333333 7位小数 // remainder let _remainder = 13 % 5; //3 println!{"32位除法:{}",_ff32} println!{"默认64位除法:{}",_quotient}; println!{"求余:{}",_remainder}; }
? 布尔类型fn main() {
let _t = true; let _f: bool = false; // with explicit type annotation }
?字符类型fn main() {
let _a = ‘z‘; let _b = ‘?‘; let _c = ‘Z‘; }
?Rust’s? 复合类型Compound types?can group multiple values into one type. Rust has two primitive compound types: tuples and arrays. 元组A tuple is a general way of grouping together some number of other values with a variety of types into one compound type. Tuples have a fixed length: once declared,they cannot grow or shrink in size. Each position in the tuple has a type,and the types of the different values in the tuple don’t have to be the same.? ? fn main() {
let _tup: (i32,f64,u8) = (500,6.4,1); let _aa = (1,2.3,"wa ka ka "); let (_x,_y,_z) = _aa; println!("The value of z is:{}",_z) }
This program first creates a tuple and binds it to the variable? ?In addition to destructuring through pattern matching,we can access a tuple element directly by using a period ( fn main() {
let _x: (i32,1); let _five_hundred = _x.0; let _six_point_four =_x.1; let _one = _x.2; println!("第三个元素:{}",_one); }
数组类型?Unlike a tuple,every element of an array must have the same type. Arrays in Rust are different from arrays in some other languages because arrays in Rust have a fixed length,like tuples. fn main() {
let _a = [1,2,3,4,5]; }
Arrays are useful when you want your data allocated on the stack rather than the heap or when you want to ensure you always have a fixed number of elements. An array isn’t as flexible as the vector type,though. A vector is a similar collection type provided by the standard library that?is?allowed to grow or shrink in size. If you’re unsure whether to use an array or a vector,you should probably use a vector.? ?It’s very unlikely that such a program will need to add or remove months,so you can use an array because you know it will always contain 12 items fn main() {
let _a = [1,5]; let _months = ["January","February","March","April","May","June","July","August","September","October","November","December"]; }
Arrays have an interesting type; it looks like this:? fn main() {
let _b: [i32; 5] = [1,5]; }
访问数组元素fn main() {
let _b: [i32; 5] = [1,5]; let _c1 = _b[0]; let _c2 = _b[1]; }
数组越界fn main() {
let a = [1,5]; let index = 10; let element = a[index]; println!("The value of element is: {}",element); }
编译阶段不会报错,在运行时会报错 [[email?protected] src]# cargo build
Compiling datatype v0.1.0 (/usr/local/automng/src/rust/test/datatype) Finished dev [unoptimized + debuginfo] target(s) in 1.39s [[email?protected] src]# cargo run Finished dev [unoptimized + debuginfo] target(s) in 0.01s Running `/usr/local/automng/src/rust/test/datatype/target/debug/datatype` thread ‘main‘ panicked at ‘index out of bounds: the len is 5 but the index is 10‘,src/main.rs:5:19 note: Run with `RUST_BACKTRACE=1` for a backtrace.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |