rustlings练习I–variable、function、if
于2022年10月14日2022年10月14日由Sukuna发布
1-1
代码语言:javascript复制// variables1.rs
// Make me compile!
// Execute `rustlings hint variables1` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
fn main() {
x = 5;
println!("x has the value {}", x);
}
这题简单,声明的方式出错了
代码语言:javascript复制// 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);
}
1-2
代码语言:javascript复制// variables2.rs
// Execute `rustlings hint variables2` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
fn main() {
let x;
if x == 10 {
println!("x is ten!");
} else {
println!("x is not ten!");
}
}
Rust不能判断x的类型,指定一下就好了
代码语言:javascript复制// variables2.rs
// Execute `rustlings hint variables2` or use the `hint` watch subcommand for a hint.
fn main() {
let x : i32 = 10;
if x == 10 {
println!("x is ten!");
} else {
println!("x is not ten!");
}
}
1-3
代码语言:javascript复制// variables3.rs
// Execute `rustlings hint variables3` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
fn main() {
let x: i32;
println!("Number {}", x);
}
x没有初始化,Rust编译器不允许访问没有初始化的元素
代码语言:javascript复制// variables3.rs
// Execute `rustlings hint variables3` or use the `hint` watch subcommand for a hint.
fn main() {
let x: i32 = 10;
println!("Number {}", x);
}
1-4
代码语言:javascript复制// variables4.rs
// Execute `rustlings hint variables4` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
fn main() {
let x = 3;
println!("Number {}", x);
x = 5; // don't change this line
println!("Number {}", x);
}
Rust默认变量不可变,x=5修改了变量,所以说会拒绝,所以说要让变量可变
代码语言:javascript复制// 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);
}
1-5
代码语言:javascript复制// variables5.rs
// Execute `rustlings hint variables5` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
fn main() {
let number = "T-H-R-E-E"; // don't change this line
println!("Spell a Number : {}", number);
number = 3; // don't rename this variable
println!("Number plus two is : {}", number 2);
}
Rust有类型检查,执行运算或者赋值时候要遵循类型的规律,但是Rust可以重新定义同名变量,变量的类型可以发生改变
代码语言:javascript复制// variables5.rs
// Execute `rustlings hint variables5` or use the `hint` watch subcommand for a hint.
fn main() {
let number = "T-H-R-E-E"; // don't change this line
println!("Spell a Number : {}", number);
let number = 3; // don't rename this variable
println!("Number plus two is : {}", number 2);
}
1-6
代码语言:javascript复制// variables6.rs
// Execute `rustlings hint variables6` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
const NUMBER = 3;
fn main() {
println!("Number {}", NUMBER);
}
const也是一样,当系统不能判断其类型的时候,需要显示说明
代码语言:javascript复制// variables6.rs
// Execute `rustlings hint variables6` or use the `hint` watch subcommand for a hint.
const NUMBER : i32 = 3;
fn main() {
println!("Number {}", NUMBER);
}
2-1
代码语言:javascript复制// functions1.rs
// Execute `rustlings hint functions1` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
fn main() {
call_me();
}
这一题我们需要自己声明一个无参数无返回值的函数.fn 名字 (){}
代码语言:javascript复制// functions1.rs
// Execute `rustlings hint functions1` or use the `hint` watch subcommand for a hint.
fn call_me(){
println!("Hello World");
}
fn main() {
call_me();
}
2-2
代码语言:javascript复制// functions2.rs
// Execute `rustlings hint functions2` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
fn main() {
call_me(3);
}
fn call_me(num:) {
for i in 0..num {
println!("Ring! Call number {}", i 1);
}
}
有参数的函数.参数在括号里面,用逗号隔开,名字:类型
代码语言:javascript复制// 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);
}
}
2-3
代码语言:javascript复制// functions3.rs
// Execute `rustlings hint functions3` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
fn main() {
call_me();
}
fn call_me(num: u32) {
for i in 0..num {
println!("Ring! Call number {}", i 1);
}
}
很难看不出来是函数调用的时候漏传实参了,补上实参即可
2-4
代码语言:javascript复制// functions4.rs
// Execute `rustlings hint functions4` or use the `hint` watch subcommand for a hint.
// This store is having a sale where if the price is an even number, you get
// 10 Rustbucks off, but if it's an odd number, it's 3 Rustbucks off.
// (Don't worry about the function bodies themselves, we're only interested
// in the signatures for now. If anything, this is a good way to peek ahead
// to future exercises!)
// I AM NOT DONE
fn main() {
let original_price = 51;
println!("Your sale price is {}", sale_price(original_price));
}
fn sale_price(price: i32) -> {
if is_even(price) {
price - 10
} else {
price - 3
}
}
fn is_even(num: i32) -> bool {
num % 2 == 0
}
函数返回返回值的方法 就是 -> 返回值类型 ,返回值可以是最后一条语句的值(这个语句不要加分号),也可以定义return
给sale_price的->后面添加上返回值类型i32即可
2-5
代码语言:javascript复制// functions5.rs
// Execute `rustlings hint functions5` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
fn main() {
let answer = square(3);
println!("The square of 3 is {}", answer);
}
fn square(num: i32) -> i32 {
num * num;
}
定义返回值不是这么定义的,一般是语句或者是return,所以说要么改成return num * num;要么去掉分号.一个块中最后一条语句去掉分号、这个可以称为块尾,就是返回值
3-1
写一个bigger函数,判断返回a和b中大的那个:
代码语言:javascript复制pub fn bigger(a: i32, b: i32) -> i32 {
// Complete this function to return the bigger number!
// Do not use:
// - another function call
// - additional variables
if a > b{
a
}
else{
b
}
}
3-2
修改这个函数,使得可以编译并且能够通过测试
代码语言:javascript复制pub fn foo_if_fizz(fizzish: &str) -> &str {
if fizzish == "fizz" {
"foo"
} else {
1
}
}
第一个是,返回的是&str,不能返回1这个数字,然后修改逻辑能够通过测试就好
代码语言:javascript复制pub fn foo_if_fizz(fizzish: &str) -> &str {
if fizzish == "fizz" {
"foo"
} else if fizzish == "fuzz" {
"bar"
}
else{
"baz"
}
}