首页 > 其他分享 >Rust 04

Rust 04

时间:2024-04-15 10:33:51浏览次数:32  
标签:04 divisible number else let condition println Rust

/// https://kaisery.github.io/trpl-zh-cn/ch03-05-control-flow.html
/// 控制流
fn main() {
   println!("Hello, world!");

   let number = 3;
   println!("First number is {number}");
   let number = 7;
   if number < 5 {
       println!("condition was true, number less than five.");
   }else{
       println!("condition was false, number greater or equals to five.");
   }

   let number = 6;
   if number % 4 == 0 {
       println!("number is divisible by 4");
   }else if number % 3 == 0 {
       println!("number is divisible by 3");
   }else if number %2 == 0 {
       println!("number is divisible by 2");
   }else{
       println!("number is not divisible by 4 , 3 or 2");
   }

   let condition = true;
   let number = if condition {5} else {6};

   println!("The value of number is: {number}");

   let mut counter = 0;
   let result = loop {
       counter += 1;
       if counter == 10 {
           break counter * 2;
       }
   };

}
///
/// https://kaisery.github.io/trpl-zh-cn/ch03-05-control-flow.html
/// 控制流
///
fn main() {
    println!("Hello, world!");
    let mut number = 3;
    while number != 0 {
        println!("{number}!");
        number -= 1;
    }

    println!("LIFTOFF!!!")
}

这里记录的是Rust的控制流,也就是判定,类似Python的语法,在if后面是不需要加括号的.

标签:04,divisible,number,else,let,condition,println,Rust
From: https://www.cnblogs.com/ukzq/p/18135336

相关文章

  • Rust 03
    /**函数体由一系列的语句和一个可选的结尾表达式构成.目前为止,我们提到的函数还不包含结尾表达式,不过作为语句一部分的表达式有一些.因为Rust是一门基于表达式(expression-based)的语言,这是一个需要理解的(不同于其他语言)重要区别.*/fnmain(){println!("Hello,wo......
  • Rust 的 Pin 机制
    背景我相信大多数人在学习Rust异步编程时都会被Futuretrait中的Pin指针感到困惑:pubtraitFuture{typeOutput;fnpoll(self:Pin<&mutSelf>,cx:&mutContext<'_>)->Poll<Self::Output>;}特别是搜索了一圈文档之后,更会对这个Pin一头雾水,彷佛自己也......
  • Rust 02
    fnmain(){println!("Hello,world!");another_function();another_function2(5);print_labeled_measurement(5,'h');}fnanother_function(){println!("Anotherfunction.")}fnanother_function2(x:i32){......
  • PHP Allowed memory size of 134217728 bytes exhausted (tried to allocate 10489856
    问题返回的json数据太大导致Allowedmemorysizeof134217728bytesexhausted(triedtoallocate10489856bytes)解决方案修改php.ini的memory_limit修改php.ini中的memory_limit数值,默认128M,不够用可以改成256M或512M宝塔中修改点击“服务”>重启或重载配置......
  • Rust 01
    //https://kaisery.github.io/trpl-zh-cn/ch03-02-data-types.htmlfnmain(){println!("Hello,world!");//标量(scalar)类型代表一个单独的值。Rust有四种基本的标量类型:整型、浮点型、布尔类型和字符类型。//你可能在其他语言中见过它们。让我们深入了解它们在......
  • 原来Rust的panic也能被捕捉?浅谈Rust的panic机制
    这一系列文章的创作目的主要是帮助我自己深入学习Rust,同时也为已经具备一定Rust编程经验,但还没有深入研究过语言和标准库的朋友提供参考。对于正在入门Rust的同学,我更建议你们看《Rust圣经》或者《TheBook》,而不是这种晦涩难懂的文章。你用过panic!宏吗?在Rust里,panic!宏可以用......
  • 一行return 写一个递归函数! 20240414
    defmake_anonymous_factorial():returnlambdan:1ifn==0elsereduce(lambdax,y:x*y,range(1,n+1))这个函数make_anonymous_factorial的目的是创建并返回一个匿名函数(也称为lambda函数),该匿名函数能够计算一个给定非负整数n的阶乘。下面是对这个函数的详细......
  • 《线性代数的本质》笔记(04-附注1-05)
    04-矩阵乘法与线性变换复合的联系问:如何描述连续两个线性变换?答:先左乘一个矩阵,再左乘一个。如果我们用一个矩阵来描述这个复合过程,那么这个矩阵应该等于两个矩阵的乘积,这就是矩阵的乘法。如何理解上图:把右侧矩阵M2看作看作第一次变换后的\(\hat{i}\)向量和\(\hat{j}\)向量,......
  • Rust所有权__Ownership
    OwnershipisasetofrulesthatgovernhowaRustprogrammanagesmemory.Allprogramshavetomanagethewaytheyuseacomputer'smemorywhileruning.Somelanguageshave garbagecollectionthatregularlylooksforno-longer-usedmemoryasthepro......
  • httprunner 4.x学习 - 04提取(extract)和校验(validate)
    前言支持2种响应结果字段提取方式:1.jmespath表达式:响应结果为JSON结构,采用jmespath表达式进行参数提取。参考教程https://jmespath.org/tutorial.html2.正则表达式(regex):返回的非JSON 格式,可以用正则表达式(regex)提取。需要具备一定的正则知识extract提取返......