当前位置: 首页 > news >正文

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.lryc.cn/news/195196.html

相关文章:

  • 【Python第三方包】快速获取硬件信息和使用情况(psutil、platform)
  • 数据结构与算法课后题-第五章(哈夫曼树和哈夫曼编码)
  • 07测试Maven中依赖的范围,依赖的传递原则,依赖排除的配置
  • 科技为饮食带来创新,看AI如何打造智能营养时代
  • 软件测试知识库+1,5款顶级自动化测试工具推荐和使用分析
  • 代码随想录算法训练营第23期day22|669. 修剪二叉搜索树、108.将有序数组转换为二叉搜索树、538.把二叉搜索树转换为累加树
  • IDEA中创建Web工程流程
  • 【论文阅读】基于卷积神经的端到端无监督变形图像配准
  • 【Rust】包和模块,文档注释,Rust格式化输出
  • leetcode221.最大正方形
  • 低代码技术这么香,如何把它的开发特点发挥到极致?
  • drawio简介以及下载安装
  • Sql Server 数据库中的所有已定义的唯一约束 (列名称 合并过了)
  • elasticsearch (六)filebeat 安装学习
  • 算法通关村第一关|青铜|链表笔记
  • 【记录】使用Python读取Tiff图像的几种方法
  • JOSEF约瑟 多档切换式漏电(剩余)继电器JHOK-ZBL1 30/100/300/500mA
  • Linux部署kubeedge 1.4
  • 第一章习题
  • nvm、node、npm解决问题过程记录
  • Linux- DWARF调试文件格式
  • 软件工程第六周
  • node+pm2安装部署
  • 大数据学习(11)-hive on mapreduce详解
  • MyBatis基础之自动映射、映射类型、文件注解双配置
  • 8、docker 安装 nginx
  • 关于Skywalking Agent customize-enhance-trace对应用复杂参数类型取值
  • 手机路径、Windows路径知识及delphiXE跨设备APP自动下载和升级
  • GitLab 502问题解决方案
  • selenium打开火狐浏览器