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);
    }
}
相关推荐
维维酱4 小时前
Rust - 读写锁 (RwLock)
rust
Source.Liu4 小时前
【PhysUnits】15.17 比例因子模块 (ratio.rs)
rust
UestcXiye5 小时前
Rust 学习笔记:关于智能指针的练习题
rust
维维酱21 小时前
Rust - 互斥锁
rust
维维酱21 小时前
Rust - 共享状态的并发
rust
ArcX1 天前
从 JS 到 Rust 的旅程
前端·javascript·rust
Humbunklung1 天前
Rust Floem UI 框架使用简介
开发语言·ui·rust
寻月隐君1 天前
深入解析 Rust 的面向对象编程:特性、实现与设计模式
后端·rust·github
KENYCHEN奉孝2 天前
基于 actix-web 框架的简单 demo
前端·rust
love530love2 天前
【笔记】旧版MSYS2 环境中 Rust 升级问题及解决过程
开发语言·人工智能·windows·笔记·python·rust·virtualenv