RustDay04------Exercise[11-20]

11.函数原型有参数时需要填写对应参数进行调用

这里原先call_me函数没有填写参数导致报错 添加一个usize即可

// functions3.rs
// Execute `rustlings hint functions3` or use the `hint` watch subcommand for a hint.fn main() {call_me(10);
}fn call_me(num: u32) {for i in 0..num {println!("Ring! Call number {}", i + 1);}
}

12.函数需要返回值

fn sale_price(price: i32) -> i32前面括号内是传入参数类型,后面是返回值类型

// 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!)fn main() {let original_price = 51;println!("Your sale price is {}", sale_price(original_price));
}fn sale_price(price: i32) -> i32{if is_even(price) {price - 10} else {price - 3}
}fn is_even(num: i32) -> bool {num % 2 == 0
}

13.函数隐式返回,不能使用逗号作为默认返回

这里square函数隐式返回num*num,如果加上分号会返回()

// functions5.rs
// Execute `rustlings hint functions5` or use the `hint` watch subcommand for a hint.// I AM NOT DONEfn main() {let answer = square(3);println!("The square of 3 is {}", answer);
}fn square(num: i32) -> i32 {num * num
}

14.使用if编写函数功能

这里使用if判断a>b的情况 然后分情况讨论

// if1.rs
// Execute `rustlings hint if1` or use the `hint` watch subcommand for a hint.pub fn bigger(a: i32, b: i32) -> i32 {// Complete this function to return the bigger number!// Do not use:// - another function call// - additional variablesif a>b {a}else {b}
}// Don't mind this for now :)
#[cfg(test)]
mod tests {use super::*;#[test]fn ten_is_bigger_than_eight() {assert_eq!(10, bigger(10, 8));}#[test]fn fortytwo_is_bigger_than_thirtytwo() {assert_eq!(42, bigger(32, 42));}
}

15.嵌套if返回条件

// if2.rs// Step 1: Make me compile!
// Step 2: Get the bar_for_fuzz and default_to_baz tests passing!
// Execute `rustlings hint if2` or use the `hint` watch subcommand for a hint.pub fn foo_if_fizz(fizzish: &str) -> &str {if fizzish == "fizz" {"foo"} else {if fizzish =="fuzz"{"bar"}else {"baz"}}
}// No test changes needed!
#[cfg(test)]
mod tests {use super::*;#[test]fn foo_for_fizz() {assert_eq!(foo_if_fizz("fizz"), "foo")}#[test]fn bar_for_fuzz() {assert_eq!(foo_if_fizz("fuzz"), "bar")}#[test]fn default_to_baz() {assert_eq!(foo_if_fizz("literally anything"), "baz")}
}

其中assert_eq!(a,b)是在比较a,b两个数值是否相等,用于做单元测试

16.使用if进行简单应用场景功能实现

自己编写calculate_price_of_apples(price:i32)->i32即可

// quiz1.rs
// This is a quiz for the following sections:
// - Variables
// - Functions
// - If// Mary is buying apples. The price of an apple is calculated as follows:
// - An apple costs 2 rustbucks.
// - If Mary buys more than 40 apples, each apple only costs 1 rustbuck!
// Write a function that calculates the price of an order of apples given
// the quantity bought. No hints this time!// Put your function here!
fn calculate_price_of_apples(price:i32)->i32 {if (price<=40){return price*2;}return price;}// Don't modify this function!
#[test]
fn verify_test() {let price1 = calculate_price_of_apples(35);let price2 = calculate_price_of_apples(40);let price3 = calculate_price_of_apples(41);let price4 = calculate_price_of_apples(65);assert_eq!(70, price1);assert_eq!(80, price2);assert_eq!(41, price3);assert_eq!(65, price4);
}

17.利用boolean类型变量做判断

// primitive_types1.rs
// Fill in the rest of the line that has code missing!
// No hints, there's no tricks, just get used to typing these :)fn main() {// Booleans (`bool`)let is_morning = true;if is_morning {println!("Good morning!");}let is_evening = false;// let // Finish the rest of this line like the example! Or make it be false!if is_evening {println!("Good evening!");}
}

18.判断字符类型

我们在这里只需要填一个字符即可,即使是emjoy

// primitive_types2.rs
// Fill in the rest of the line that has code missing!
// No hints, there's no tricks, just get used to typing these :)fn main() {// Characters (`char`)// Note the _single_ quotes, these are different from the double quotes// you've been seeing around.let my_first_initial = 'C';if my_first_initial.is_alphabetic() {println!("Alphabetical!");} else if my_first_initial.is_numeric() {println!("Numerical!");} else {println!("Neither alphabetic nor numeric!");}let your_character='u';// Finish this line like the example! What's your favorite character?// Try a letter, try a number, try a special character, try a character// from a different language than your own, try an emoji!if your_character.is_alphabetic() {println!("Alphabetical!");} else if your_character.is_numeric() {println!("Numerical!");} else {println!("Neither alphabetic nor numeric!");}
}

19.获取字符串长度

// primitive_types3.rs
// Create an array with at least 100 elements in it where the ??? is.
// Execute `rustlings hint primitive_types3` or use the `hint` watch subcommand for a hint.fn main() {let a = "99999999999999999999999999999999";if a.len() >= 100 {println!("Wow, that's a big array!");} else {println!("Meh, I eat arrays like that for breakfast.");}
}

20.字符串切片

使用&引用变量 [leftIndex..rightIndex)区间内切片

// primitive_types4.rs
// Get a slice out of Array a where the ??? is so that the test passes.
// Execute `rustlings hint primitive_types4` or use the `hint` watch subcommand for a hint.#[test]
fn slice_out_of_array() {let a = [1, 2, 3, 4, 5];let nice_slice = &a[1..4];assert_eq!([2, 3, 4], nice_slice)
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/105249.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

亚马逊测评安全吗?

测评可以说是卖家非常宝贵的财富&#xff0c;通过测评和广告相结合&#xff0c;可以快速有效的提升店铺的产品销量&#xff0c;提高转化&#xff0c;提升listing权重&#xff0c;但现在很多卖家找真人测评补单后店铺出现问题导致大家对测评的安全性感到担忧&#xff0c;因为真人…

List 模拟实现

前言 本文将会向你介绍如何模拟实现list、iterator迭代器 模拟实现 引入 迭代器是一种用于访问容器中元素的对象&#xff0c;它封装了对容器中元素的访问方式。迭代器提供了一组操作接口&#xff0c;可以让我们通过迭代器对象来遍历容器中的元素。&#xff08;iterator迭代器…

Lua调用C#类

先创建一个Main脚本作为主入口&#xff0c;挂载到摄像机上 public class Main : MonoBehaviour {// Start is called before the first frame updatevoid Start(){LuaMgr.GetInstance().Init();LuaMgr.GetInstance().DoLuaFile("Main");}// Update is called once p…

【WebRTC---源码篇】(十:零)WEBRTC/StreamStatisticianImpl持续更新中)

StreamStatisticianImpl是WebRTC的一个内部实现类&#xff0c;用于统计和管理媒体流的各种统计信息。 StreamStatisticianImpl负责记录和计算以下统计数据&#xff1a; 1. 带宽统计&#xff1a;记录媒体流的发送和接收带宽信息&#xff0c;包括发送比特率、接收比特率、发送丢…

关于SpringBoot2.x集成SpringSecurity+JJWT(0.7.0-->0.11.5)生成Token登录鉴权的问题

项目场景&#xff1a; 问题&#xff1a;遵循版本稳定的前提下&#xff0c;搭建权限认证框架&#xff0c;基于SpringBoot2.xSpringSecurity向上依赖jjwt0.7.0构建用户认证鉴权&#xff0c;起因是某L觉得jjwt0.7.0版本&#xff0c;官方已经放弃维护&#xff0c;且从maven仓库对0…

CocosCreator 面试题(十二)Cocos Creator Label 的原理以及如何减少Drawcall

在Cocos Creator中&#xff0c;Label是用于显示文本的组件。它的原理是通过将文本渲染到纹理上&#xff0c;并将纹理贴图显示在屏幕上来实现。 一、Label组件的工作原理 字体加载&#xff1a;首先&#xff0c;Label组件需要加载所需的字体文件。可以通过在编辑器中设置字体资源…

python二次开发CATIA:测量曲线长度

以下代码是使用Python语言通过win32com库来控制CATIA应用程序的一个示例。主要步骤包括创建一个新的Part文件&#xff0c;然后在其中创建一个新的几何图形集&#xff0c;并在这个集合中创建一个样条线。这个样条线是通过一组给定的坐标点来创建的&#xff0c;这些点被添加到集合…

【SQL】NodeJs 连接 MySql 、MySql 常见语句

1.安装 mysql npm install mysql 2.引入MySql import mysql from mysql 3.连接MySql const connection mysql.createConnection({host: yourServerip,user: yourUsername,password: yourPassword,database: yourDatabase })connection.connect(err > {if (err) {console…

SpringCloud-Config

一、介绍 &#xff08;1&#xff09;服务注册中心 &#xff08;2&#xff09;管理各个服务上的application.yml&#xff0c;支持动态修改&#xff0c;但不会影响客户端配置 &#xff08;3&#xff09;一般将application.yml文件放在git上&#xff0c;客户端通过http/https方式…

MyLife - Docker安装rabbitmq

Docker安装rabbitmq 个人觉得像rabbitmq之类的基础设施在线上环境直接物理机安装使用可能会好些。但是在开发测试环境用docker容器还是比较方便的。这里学习下docker安装rabbitmq使用。 1. rabbitmq 镜像库地址 rabbitmq 镜像库地址&#xff1a;https://hub.docker.com/_/rabbi…

介绍一款小巧的Excel比对工具-DiffExcel

【缘起&#xff1a;此前找了一通&#xff0c;没有找到免费又好用的Excel比对工具&#xff0c;而ExcelBDD需要把Excel文件存放到Git&#xff0c;因此迫切需要Excel比对工具。 最新升级到V1.3.3&#xff0c;因为git diff有变化&#xff0c;原来是git diff会修改文件名&#xff0…

Compose 组件 - 分页器 HorizontalPager、VerticalPager

一、概念 类似于 ViewPager&#xff0c;1.4 版本之前需要借助 accompanis 库&#xff0c;底层基于 LazyColumn、LazyRow 实现&#xff0c;在使用上也基本相同。默认情况下 HorizontalPager 占据屏幕的整个宽度&#xff0c;VerticalPager 会占据整个高度。 fun HorizontalPager(…

xshell使用方法(超详细)

一、安装 下载最新版安装即可&#xff0c;不需要做任何配置。 安装完成后输入账号名和邮箱&#xff0c;确认后邮箱会收到一条确认邮件&#xff0c;将里面的链接点开即可免费使用&#xff08;仅安装后会出现&#xff0c;认证后以后再打开不需要重复操作&#xff0c;如果重新安…

【MySQL】索引的作用及知识储备

为什么要有索引 索引可以提高数据库的性能。不用加内存&#xff0c;不用改程序&#xff0c;不用调sql&#xff0c;只要执行正确的create indix&#xff0c;查询的速度就可能提高成百上千倍。但相应的代价是&#xff0c;插入&#xff0c;更新&#xff0c;删除的速度有所减弱。 …

[论文分享] EnBinDiff: Identifying Data-Only Patches for Binaries

EnBinDiff: Identifying Data-Only Patches for Binaries [TDSC 2021] 在本文中&#xff0c;我们将重点介绍纯数据补丁&#xff0c;这是一种不会引起任何结构更改的特定类型的安全补丁。作为导致假阴性的最重要原因之一&#xff0c;纯数据补丁成为影响所有最先进的二进制差分方…

切换npm的版本

1、在配置环境变量的地址中&#xff0c;多准备几个已解压版本的node 2、要想升降版本直接更改该文件中的文件夹名称就行 环境变量中的path的值是不用变的C:\Program Files\nodejs

Ubuntu22安装Docker engine(apt安装方式)

一、准备工作 新创建一个虚拟机。 进入虚拟机&#xff1a; 二、安装docker docker现在对用不同主机提供了不同安装包&#xff1a;docker engine 和 docker desktop。 docker desktop适用于图形化的桌面电脑&#xff0c;docker engine适用于服务器。我们这里当然是安装docker…

SpringCloud-Gateway

一、介绍 &#xff08;1&#xff09;网关服务 &#xff08;2&#xff09;功能&#xff1a;断言、路由、过滤 &#xff08;3&#xff09;能避免用户直接访问到业务主机 二、项目搭建 a、编写pom.xml&#xff08;注意移除web框架&#xff0c;gateway中自带有&#xff09; &l…

7.定时器

定时器资源 CC2530有四个定时器TIM1~TIM4和休眠定时器 TIM1 定时器1 是一个独立的16 位定时器&#xff0c;支持典型的定时/计数功能&#xff0c;比如输入捕获&#xff0c;输出比较和PWM 功能。定时器有五个独立的捕获/比较通道。每个通道定时器使用一个I/O 引脚。定时器用于…

C# 获取入参函数名

前言 C# 如何通过形参获得实参的名字&#xff1f; C# 10 新特性 —— CallerArgumentExpression 我最近在研究Godot&#xff0c;想简化Godot的操作。所有有了这个需求 public static void Test(string str){console.wirteLine(nameof(str)); } public static void Main(string…