基本类型
Rust 基本数据类型
在
let guess: u32 = "42".parse().expect("Not a number!");
这里如果不添加类型注解,
error[E0282]: type annotations needed
--> src/main.rs:2:9
|
2 | let guess = "42".parse().expect("Not a number!");
| ^^^^^
| |
| cannot infer type for `_`
| consider giving `guess` a type
复合类型(Compound types)可以将多个值组合成一个类型。
Copy types
fn main() {
let my_number = 8;
prints_number(my_number); // Prints 8. prints_number gets a copy of my_number
prints_number(my_number); // Prints 8 again.
// No problem, because my_number is copy type!
}
fn prints_number(number: i32) { // No return with ->
// If number was not copy type, it would take it
// and we couldn't use it again
println!("{}", number);
}
布尔型
正如其他大部分编程语言一样,
fn main() {
let t = true;
let f: bool = false; // 显式指定类型注解
}
使用布尔值的主要场景是条件表达式,例如