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


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

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

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

    12.函数需要返回值

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

    1. // functions4.rs
    2. // Execute `rustlings hint functions4` or use the `hint` watch subcommand for a hint.
    3. // This store is having a sale where if the price is an even number, you get
    4. // 10 Rustbucks off, but if it's an odd number, it's 3 Rustbucks off.
    5. // (Don't worry about the function bodies themselves, we're only interested
    6. // in the signatures for now. If anything, this is a good way to peek ahead
    7. // to future exercises!)
    8. fn main() {
    9. let original_price = 51;
    10. println!("Your sale price is {}", sale_price(original_price));
    11. }
    12. fn sale_price(price: i32) -> i32{
    13. if is_even(price) {
    14. price - 10
    15. } else {
    16. price - 3
    17. }
    18. }
    19. fn is_even(num: i32) -> bool {
    20. num % 2 == 0
    21. }

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

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

    1. // functions5.rs
    2. // Execute `rustlings hint functions5` or use the `hint` watch subcommand for a hint.
    3. // I AM NOT DONE
    4. fn main() {
    5. let answer = square(3);
    6. println!("The square of 3 is {}", answer);
    7. }
    8. fn square(num: i32) -> i32 {
    9. num * num
    10. }

    14.使用if编写函数功能

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

    1. // if1.rs
    2. // Execute `rustlings hint if1` or use the `hint` watch subcommand for a hint.
    3. pub fn bigger(a: i32, b: i32) -> i32 {
    4. // Complete this function to return the bigger number!
    5. // Do not use:
    6. // - another function call
    7. // - additional variables
    8. if a>b {
    9. a
    10. }
    11. else {
    12. b
    13. }
    14. }
    15. // Don't mind this for now :)
    16. #[cfg(test)]
    17. mod tests {
    18. use super::*;
    19. #[test]
    20. fn ten_is_bigger_than_eight() {
    21. assert_eq!(10, bigger(10, 8));
    22. }
    23. #[test]
    24. fn fortytwo_is_bigger_than_thirtytwo() {
    25. assert_eq!(42, bigger(32, 42));
    26. }
    27. }

    15.嵌套if返回条件

    1. // if2.rs
    2. // Step 1: Make me compile!
    3. // Step 2: Get the bar_for_fuzz and default_to_baz tests passing!
    4. // Execute `rustlings hint if2` or use the `hint` watch subcommand for a hint.
    5. pub fn foo_if_fizz(fizzish: &str) -> &str {
    6. if fizzish == "fizz" {
    7. "foo"
    8. } else {
    9. if fizzish =="fuzz"{
    10. "bar"
    11. }
    12. else {
    13. "baz"
    14. }
    15. }
    16. }
    17. // No test changes needed!
    18. #[cfg(test)]
    19. mod tests {
    20. use super::*;
    21. #[test]
    22. fn foo_for_fizz() {
    23. assert_eq!(foo_if_fizz("fizz"), "foo")
    24. }
    25. #[test]
    26. fn bar_for_fuzz() {
    27. assert_eq!(foo_if_fizz("fuzz"), "bar")
    28. }
    29. #[test]
    30. fn default_to_baz() {
    31. assert_eq!(foo_if_fizz("literally anything"), "baz")
    32. }
    33. }

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

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

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

    1. // quiz1.rs
    2. // This is a quiz for the following sections:
    3. // - Variables
    4. // - Functions
    5. // - If
    6. // Mary is buying apples. The price of an apple is calculated as follows:
    7. // - An apple costs 2 rustbucks.
    8. // - If Mary buys more than 40 apples, each apple only costs 1 rustbuck!
    9. // Write a function that calculates the price of an order of apples given
    10. // the quantity bought. No hints this time!
    11. // Put your function here!
    12. fn calculate_price_of_apples(price:i32)->i32 {
    13. if (price<=40){
    14. return price*2;
    15. }
    16. return price;
    17. }
    18. // Don't modify this function!
    19. #[test]
    20. fn verify_test() {
    21. let price1 = calculate_price_of_apples(35);
    22. let price2 = calculate_price_of_apples(40);
    23. let price3 = calculate_price_of_apples(41);
    24. let price4 = calculate_price_of_apples(65);
    25. assert_eq!(70, price1);
    26. assert_eq!(80, price2);
    27. assert_eq!(41, price3);
    28. assert_eq!(65, price4);
    29. }

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

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

    18.判断字符类型

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

    1. // primitive_types2.rs
    2. // Fill in the rest of the line that has code missing!
    3. // No hints, there's no tricks, just get used to typing these :)
    4. fn main() {
    5. // Characters (`char`)
    6. // Note the _single_ quotes, these are different from the double quotes
    7. // you've been seeing around.
    8. let my_first_initial = 'C';
    9. if my_first_initial.is_alphabetic() {
    10. println!("Alphabetical!");
    11. } else if my_first_initial.is_numeric() {
    12. println!("Numerical!");
    13. } else {
    14. println!("Neither alphabetic nor numeric!");
    15. }
    16. let your_character='u';// Finish this line like the example! What's your favorite character?
    17. // Try a letter, try a number, try a special character, try a character
    18. // from a different language than your own, try an emoji!
    19. if your_character.is_alphabetic() {
    20. println!("Alphabetical!");
    21. } else if your_character.is_numeric() {
    22. println!("Numerical!");
    23. } else {
    24. println!("Neither alphabetic nor numeric!");
    25. }
    26. }

    19.获取字符串长度

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

    20.字符串切片

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

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

  • 相关阅读:
    HT5169 单声道D类音频功放 I2S输入
    2022年8月15日陌陌推荐算法工程师面试题5道|含解
    MFC基础入门
    巯基功能化相思子毒素;Thiol-Abrin Rhodamine Conjugate(AT);SH-AT
    【Unity3D】魔方
    Gradio Dataframe 学习笔记
    2021年9月电子学会图形化一级编程题解析含答案:小狗进圈
    【ElasticSearch】HTTP调用API
    ansible 002 连接被控端 inventory ansible.cfg ansible-adhoc ansible原理
    打印机清零操作步骤分享!
  • 原文地址:https://blog.csdn.net/m0_72678953/article/details/133822582