前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >rustlings练习I–variable、function、if

rustlings练习I–variable、function、if

作者头像
用户7267083
发布2022-12-08 15:09:46
4120
发布2022-12-08 15:09:46
举报
文章被收录于专栏:sukuna的博客sukuna的博客

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"
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022年10月14日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • rustlings练习I–variable、function、if
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档