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

1.做题须知

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

rust 复制代码
// intro1.rs
// About this `I AM NOT DONE` thing:
// We sometimes encourage you to keep trying things on a given exercise, even
// after you already figured it out. If you got everything working and feel
// ready for the next exercise, remove the `I AM NOT DONE` comment below.
// Execute `rustlings hint intro1` or use the `hint` watch subcommand for a hint.
//
// If you're running this using `rustlings watch`: The exercise file will be reloaded
// when you change one of the lines below! Try adding a `println!` line, or try changing
// what it outputs in your terminal. Try removing a semicolon and see what happens!

// I AM NOT DONE

fn main() {
    println!("Hello and");
    println!(r#"       welcome to...                      "#);
    println!(r#"                 _   _ _                  "#);
    println!(r#"  _ __ _   _ ___| |_| (_)_ __   __ _ ___  "#);
    println!(r#" | '__| | | / __| __| | | '_ \ / _` / __| "#);
    println!(r#" | |  | |_| \__ \ |_| | | | | | (_| \__ \ "#);
    println!(r#" |_|   \__,_|___/\__|_|_|_| |_|\__, |___/ "#);
    println!(r#"                               |___/      "#);
    println!();
    println!("This exercise compiles successfully. The remaining exercises contain a compiler");
    println!("or logic error. The central concept behind Rustlings is to fix these errors and");
    println!("solve the exercises. Good luck!");
    println!();
    println!("The source for this exercise is in `exercises/intro/intro1.rs`. Have a look!");
    println!("Going forward, the source of the exercises will always be in the success/failure output.");
}

2.基本输出

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

rust 复制代码
// intro2.rs
// Make the code print a greeting to the world.
// Execute `rustlings hint intro2` or use the `hint` watch subcommand for a hint.



fn main() {
    println!("Hello {:?}!","world");
}

3.变量绑定

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

rust 复制代码
// variables1.rs
// Make me compile!
// Execute `rustlings hint variables1` or use the `hint` watch subcommand for a hint.



fn main() {
    let x = 5;
    println!("x has the value {}", x);
}

4.变量类型

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

rust 复制代码
// variables2.rs
// Execute `rustlings hint variables2` or use the `hint` watch subcommand for a hint.



fn main() {
    let x=3;
    if x == 10 {
        println!("x is ten!");
    } else {
        println!("x is not ten!");
    }
}

5.指定变量类型

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

rust 复制代码
// variables3.rs
// Execute `rustlings hint variables3` or use the `hint` watch subcommand for a hint.


fn main() {
    let x: i32=3;
    println!("Number {}", x);
}

6.创建可变类型变量

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

rust 复制代码
// variables4.rs
// Execute `rustlings hint variables4` or use the `hint` watch subcommand for a hint.



fn main() {
    let mut x = 3;
    println!("Number {}", x);
    x = 5; // don't change this line
    println!("Number {}", x);
}

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

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

rust 复制代码
// variables5.rs
// Execute `rustlings hint variables5` or use the `hint` watch subcommand for a hint.



fn main() {
    let mut number = "T-H-R-E-E"; // don't change this line
    println!("Spell a Number : {}", number);
    let number:i32= 3; // don't rename this variable
    println!("Number plus two is : {:?}", number );
}

8.常量申明

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

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

rust 复制代码
// variables6.rs
// Execute `rustlings hint variables6` or use the `hint` watch subcommand for a hint.



const NUMBER:i32 = 3;
fn main() {
    println!("Number {}", NUMBER);
}

9.书写函数

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

rust 复制代码
// functions1.rs
// Execute `rustlings hint functions1` or use the `hint` watch subcommand for a hint.


fn call_me(){
    println!("Hello Rust");
}
fn main() {
    call_me();
}

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

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

rust 复制代码
// functions2.rs
// Execute `rustlings hint functions2` or use the `hint` watch subcommand for a hint.



fn main() {
    call_me(3);
}

fn call_me(num:i32) {
    for i in 0..num {
        println!("Ring! Call number {}", i + 1);
    }
}
相关推荐
花褪残红青杏小9 小时前
Rust图像处理第6节- 均值模糊 & 中值模糊:3×3 邻域的两种经典玩法
rust·webassembly·图形学
子兮曰14 小时前
前端工具链的「Rust 化」:一场没有赢家的军备竞赛?
前端·后端·rust
星栈16 小时前
写 Dioxus Demo 不难,难的是把它写成项目
前端·rust·前端框架
mCell17 小时前
【锐评】桌面端技术营销:别拿跑分当工程判断
前端·rust·electron
武子康1 天前
调查研究-201 Rust 里的 dev build 和 release build:为什么同一份代码性能差这么多?
后端·架构·rust
doiito1 天前
【Agent Harness】Gliding Horse 的 L2 作战地图:让多 Agent 协作从“摸黑”变成“透明”
ai·rust·架构设计·系统设计·ai agent
星栈2 天前
我用 Rust + Dioxus 做了个全栈跨平台笔记应用:再把新建、编辑和交付补上
前端·rust·前端框架
独孤留白2 天前
从C到Rust:基本类型 C 的隐式不确定 vs Rust 的显式确定
rust
清晨很温柔啊2 天前
# 用 Rust 手搓 AI 自演化主板:当 18 个异构器官长出 C++ 骨骼
rust
星栈3 天前
我用 Rust + Dioxus 做了个全栈跨平台笔记应用:第一版先把列表和详情跑通
前端·rust·前端框架