main.rs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // 文件名随意,入口main函数,
  2. // 编译: rustc main.rs
  3. // 运行1: cargo run --package rust-quickstart --bin rust-quickstart
  4. // 运行2:./main
  5. fn main() {
  6. println!("Hello, world!");
  7. // 不可变变量
  8. let a: i32 = 2;
  9. // 可变变量
  10. let mut b = 123;
  11. b = 456;
  12. println!(" a is {}, a again is {}", a, a);
  13. // let 重载
  14. let x = 5;
  15. let x = x + 1;
  16. let x = x * 2;
  17. println!("The value of x is: {}", x);
  18. // 数学运算
  19. let _sum = 5 + 10; // 加
  20. let _difference = 95.5 - 4.3; // 减
  21. let _product = 4 * 30; // 乘
  22. let _quotient = 56.7 / 32.2; // 除
  23. let _remainder = 43 % 5; // 求余
  24. let _arr_a: [i32; 6] = [1, 2, 3, 3, 4, 5];
  25. let number = 3;
  26. if number < 5 {
  27. println!("less than 5");
  28. } else {
  29. println!("more than 5");
  30. }
  31. while number != 4 {
  32. println!("number is : {}", number);
  33. }
  34. let mut i = 0;
  35. while i < 10 {
  36. i += 1;
  37. println!("i is {}", i);
  38. }
  39. for i in _arr_a.iter() {
  40. println!("值为:{}", i);
  41. }
  42. // 无限循环 while(true)
  43. // loop {
  44. // }
  45. // 切片
  46. let s = String::from("broadcast");
  47. let part1 = &s[0..5];
  48. let part2 = &s[5..9];
  49. println!("{}={}+{}", s, part1, part2);
  50. }