• RustDay04------Exercise[01-10]


    1.做题须知

    这一题告诉我们可以尝试修改下面的输出,在觉得OK之后删除// I AM NOT DONE注释即可进入下一题

    1. // intro1.rs
    2. // About this `I AM NOT DONE` thing:
    3. // We sometimes encourage you to keep trying things on a given exercise, even
    4. // after you already figured it out. If you got everything working and feel
    5. // ready for the next exercise, remove the `I AM NOT DONE` comment below.
    6. // Execute `rustlings hint intro1` or use the `hint` watch subcommand for a hint.
    7. //
    8. // If you're running this using `rustlings watch`: The exercise file will be reloaded
    9. // when you change one of the lines below! Try adding a `println!` line, or try changing
    10. // what it outputs in your terminal. Try removing a semicolon and see what happens!
    11. // I AM NOT DONE
    12. fn main() {
    13. println!("Hello and");
    14. println!(r#" welcome to... "#);
    15. println!(r#" _ _ _ "#);
    16. println!(r#" _ __ _ _ ___| |_| (_)_ __ __ _ ___ "#);
    17. println!(r#" | '__| | | / __| __| | | '_ \ / _` / __| "#);
    18. println!(r#" | | | |_| \__ \ |_| | | | | | (_| \__ \ "#);
    19. println!(r#" |_| \__,_|___/\__|_|_|_| |_|\__, |___/ "#);
    20. println!(r#" |___/ "#);
    21. println!();
    22. println!("This exercise compiles successfully. The remaining exercises contain a compiler");
    23. println!("or logic error. The central concept behind Rustlings is to fix these errors and");
    24. println!("solve the exercises. Good luck!");
    25. println!();
    26. println!("The source for this exercise is in `exercises/intro/intro1.rs`. Have a look!");
    27. println!("Going forward, the source of the exercises will always be in the success/failure output.");
    28. }

    2.基本输出

    我们使用{:?}通配输出一下就好了

    1. // intro2.rs
    2. // Make the code print a greeting to the world.
    3. // Execute `rustlings hint intro2` or use the `hint` watch subcommand for a hint.
    4. fn main() {
    5. println!("Hello {:?}!","world");
    6. }

    3.变量绑定

    由于Rust里面的变量完全属于主人,所以我们需要使用let语句来对变量进行绑定

    1. // variables1.rs
    2. // Make me compile!
    3. // Execute `rustlings hint variables1` or use the `hint` watch subcommand for a hint.
    4. fn main() {
    5. let x = 5;
    6. println!("x has the value {}", x);
    7. }

    4.变量类型

    这一题告诉我们变量需要类型,给类型只需要我们对其赋值即可,rust会自动分配他的类型

    1. // variables2.rs
    2. // Execute `rustlings hint variables2` or use the `hint` watch subcommand for a hint.
    3. fn main() {
    4. let x=3;
    5. if x == 10 {
    6. println!("x is ten!");
    7. } else {
    8. println!("x is not ten!");
    9. }
    10. }

    5.指定变量类型

    我们需要对变量初始化赋值才能使用,而且可以使用:i32指定变量为32位整数

    1. // variables3.rs
    2. // Execute `rustlings hint variables3` or use the `hint` watch subcommand for a hint.
    3. fn main() {
    4. let x: i32=3;
    5. println!("Number {}", x);
    6. }

    6.创建可变类型变量

    不设置变量为可变类型,就会在改变时报错

    1. // variables4.rs
    2. // Execute `rustlings hint variables4` or use the `hint` watch subcommand for a hint.
    3. fn main() {
    4. let mut x = 3;
    5. println!("Number {}", x);
    6. x = 5; // don't change this line
    7. println!("Number {}", x);
    8. }

    7.重新绑定(变量遮蔽)

    当需要用一个变量读取不同类型的数据的时候 可以使用let+数据类型重新创建一个

    1. // variables5.rs
    2. // Execute `rustlings hint variables5` or use the `hint` watch subcommand for a hint.
    3. fn main() {
    4. let mut number = "T-H-R-E-E"; // don't change this line
    5. println!("Spell a Number : {}", number);
    6. let number:i32= 3; // don't rename this variable
    7. println!("Number plus two is : {:?}", number );
    8. }

    8.常量申明

    常量不仅仅默认不可变,而且自始至终不可变,因为常量在编译完成后,已经确定它的值。

    常量使用 const 关键字而不是 let 关键字来声明,并且值的类型必须标注

    1. // variables6.rs
    2. // Execute `rustlings hint variables6` or use the `hint` watch subcommand for a hint.
    3. const NUMBER:i32 = 3;
    4. fn main() {
    5. println!("Number {}", NUMBER);
    6. }

    9.书写函数

    使用fn funName(){}来创建一个函数申明,然后在main函数里面调用

    1. // functions1.rs
    2. // Execute `rustlings hint functions1` or use the `hint` watch subcommand for a hint.
    3. fn call_me(){
    4. println!("Hello Rust");
    5. }
    6. fn main() {
    7. call_me();
    8. }

    10.函数传入形参需要指定类型

    形参需要申明传入的变量的类型

    1. // functions2.rs
    2. // Execute `rustlings hint functions2` or use the `hint` watch subcommand for a hint.
    3. fn main() {
    4. call_me(3);
    5. }
    6. fn call_me(num:i32) {
    7. for i in 0..num {
    8. println!("Ring! Call number {}", i + 1);
    9. }
    10. }

  • 相关阅读:
    SpringFramework 之EnableAsync
    Docker Desktop 配置阿里云镜像加速
    react有哪些性能优化的手段?
    【FreeSwitch开发实践】FreeSwitch常用知识点总结
    Kafka-基础
    es6总结
    跨平台`ChatGpt` 客户端
    [附源码]Python计算机毕业设计电影网站系统设计
    wordcloud—根据文本生成词云—Python第三方库
    JavaScriptJQuery_jQuery链式编程
  • 原文地址:https://blog.csdn.net/m0_72678953/article/details/133820967