1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- // 文件名随意,入口main函数,
- // 编译: rustc main.rs
- // 运行1: cargo run --package rust-quickstart --bin rust-quickstart
- // 运行2:./main
- fn main() {
- println!("Hello, world!");
- // 不可变变量
- let a: i32 = 2;
- // 可变变量
- let mut b = 123;
- b = 456;
- println!(" a is {}, a again is {}", a, a);
- // let 重载
- let x = 5;
- let x = x + 1;
- let x = x * 2;
- println!("The value of x is: {}", x);
- // 数学运算
- let _sum = 5 + 10; // 加
- let _difference = 95.5 - 4.3; // 减
- let _product = 4 * 30; // 乘
- let _quotient = 56.7 / 32.2; // 除
- let _remainder = 43 % 5; // 求余
- let _arr_a: [i32; 6] = [1, 2, 3, 3, 4, 5];
- let number = 3;
- if number < 5 {
- println!("less than 5");
- } else {
- println!("more than 5");
- }
- while number != 4 {
- println!("number is : {}", number);
- }
- let mut i = 0;
- while i < 10 {
- i += 1;
- println!("i is {}", i);
- }
- for i in _arr_a.iter() {
- println!("值为:{}", i);
- }
- // 无限循环 while(true)
- // loop {
- // }
- // 切片
- let s = String::from("broadcast");
- let part1 = &s[0..5];
- let part2 = &s[5..9];
- println!("{}={}+{}", s, part1, part2);
- }
|