RustDay06------Exercise[81-90]

81.宏函数里面的不同的匹配规则需要使用分号隔开

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

// I AM NOT DONE

#[rustfmt::skip]
macro_rules! my_macro {
    () => {
        println!("Check out my macro!");
    };
    ($val:expr) => {
        println!("Look at this other macro: {}", $val);
    };
}

fn main() {
    my_macro!();
    my_macro!(7777);
}

82.使用内部封装好的宏来替代某些常数

rust 复制代码
// clippy1.rs
//
// The Clippy tool is a collection of lints to analyze your code so you can
// catch common mistakes and improve your Rust code.
//
// For these exercises the code will fail to compile when there are clippy
// warnings check clippy's suggestions from the output to solve the exercise.
//
// Execute `rustlings hint clippy1` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

use std::f32;

fn main() {
    let pi = f32::consts::PI;
    // 3.14f32;
    let radius = 5.00f32;

    let area = pi * f32::powi(radius, 2);

    println!(
        "The area of a circle with radius {:.2} is {:.5}!",
        radius, area
    )
}

83.if let回顾

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

// I AM  DONE

fn main() {
    let mut res = 42;
    let option = Some(12);
    if let Some(x)=option {
        res += x;
    }
    println!("{}", res);
}

84.按照提示修改

这题说明 resize是原地操作 没有返回值

交换值必须要借助中间变量

rust 复制代码
// clippy3.rs
// 
// Here's a couple more easy Clippy fixes, so you can see its utility.
//
// Execute `rustlings hint clippy3` or use the `hint` watch subcommand for a hint.

// I AM NOT DONE

#[allow(unused_variables, unused_assignments)]
fn main() {
    let my_option: Option<()> = None;
    // if my_option.is_none() {
    //     my_option.unwrap();
    // }

    let my_arr = &[
        -1, -2, -3,
        -4, -5, -6,
    ];
    println!("My array! Here it is: {:?}", my_arr);

    let my_empty_vec = vec![1, 2, 3, 4, 5];
    println!("This Vec is empty, see? {:?}", my_empty_vec);

    let mut value_a = 45;
    let mut value_b = 66;
    // Let's swap these two!
    let mut temp =0;
    temp=value_a;
    value_a = value_b;
    value_b = temp;
    println!("value a: {}; value b: {}", value_a, value_b);
}

85.使用as强制转换类型

rust 复制代码
// using_as.rs
//
// Type casting in Rust is done via the usage of the `as` operator. Please note
// that the `as` operator is not only used when type casting. It also helps with
// renaming imports.
//
// The goal is to make sure that the division does not fail to compile and
// returns the proper type.
//
// Execute `rustlings hint using_as` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

fn average(values: &[f64]) -> f64 {
    let total = values.iter().sum::<f64>();
    total / values.len() as f64
}

fn main() {
    let values = [3.5, 0.3, 13.0, 11.7];
    println!("{}", average(&values));
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn returns_proper_type_and_value() {
        assert_eq!(average(&[3.5, 0.3, 13.0, 11.7]), 7.125);
    }
}
相关推荐
前端小魔女3 小时前
2024-我赚到自媒体第一桶金
前端·rust
用户46946515978311 小时前
Rust使用tracing记录日志
rust
编码浪子12 小时前
构建一个rust生产应用读书笔记7-确认邮件2
开发语言·后端·rust
songroom18 小时前
Rust: offset祼指针操作
开发语言·算法·rust
唐 城1 天前
curl 放弃对 Hyper Rust HTTP 后端的支持
开发语言·http·rust
从善若水1 天前
【2024】Merry Christmas!一起用Rust绘制一颗圣诞树吧
开发语言·后端·rust
gerrylon0071 天前
rust学习: 有用的命令
rust
brrdg_sefg2 天前
Rust 在前端基建中的使用
前端·rust·状态模式
m0_748230942 天前
Rust赋能前端: 纯血前端将 Table 导出 Excel
前端·rust·excel
SomeB1oody2 天前
【Rust自学】6.1. 定义枚举
开发语言·后端·rust