首页 > 其他分享 >[Rust] Create an array of numbers by using Range

[Rust] Create an array of numbers by using Range

时间:2024-02-23 19:11:54浏览次数:18  
标签:slice .. Create Range let numbers array

fn main() {
    let a = 0..100;

    if a.len() >= 100 {
        println!("Wow, that's a big array!");
    } else {
        println!("Meh, I eat arrays like that for breakfast.");
        panic!("Array not big enough, more elements needed")
    }
}

 

Example2:

#[test]
fn slice_out_of_array() {
    let a = [1, 2, 3, 4, 5];

    let nice_slice = &a[1..4]; // need to use borrow syntax

    assert_eq!([2, 3, 4], nice_slice)
}

 

标签:slice,..,Create,Range,let,numbers,array
From: https://www.cnblogs.com/Answer1215/p/18030231

相关文章

  • [Rust] Create a loop in Rust
    ThislessonshowshowtouseaRust loop torunaprograminfinitely. usestd::io;usestd::process;fnmain(){loop{println!("Pleaseenterafirstnumber:");leta=read_user_input();println!("Plea......
  • CreateHolesInImage说明文档-对于遥感影像的空洞创建多边形矢量数据
    提取遥感影像的空洞地理处理工具箱特点:通用地理处理工具,支持任何遥感影像,包括无人机,卫星遥感,普通图片和gdb,mdb数据库等。速度快,极致效率,效率高,支持对多个文件夹下的任意多数据进行批处理使用简单,全自动话,无人工干预功能:提取空洞提取空洞和非空洞默认临时文件夹,结果文件夹默认临时......
  • 02. Create Project 创建项目导入素材
    使用版本我因为需要开发微信小游戏,所以使用的是2022.3.8f1c1版本创建项目首先创建一个3D项目,因为后续我们需要学习如何将一个普通的项目升级为URP通用渲染管线安装URP通用渲染管线在PackageManager中搜索Universal,然后安装UniversalRP。2022版本的Unity不需......
  • Go 100 mistakes - #32: Ignoring the impact of using pointer elements in range lo
    Thissectionlooksataspecificmistakewhenusingarangeloopwithpointerelements.Ifwe’renotcautiousenough,itcanleadustoanissuewherewereferencethe wrongelements.Let’sexaminethisproblemandhowtofixit.Beforewebegin,let’scla......
  • The number of divisors(约数) about Humble Numbers
    Anumberwhoseonlyprimefactorsare2,3,5or7iscalledahumblenumber.Thesequence1,2,3,4,5,6,7,8,9,10,12,14,15,16,18,20,21,24,25,27,...showsthefirst20humblenumbers.Nowgivenahumblenumber,pleasewriteaprogramtocal......
  • Humble Numbers
    打的暴力,样例都要过好久,小脑直接萎缩©©VjudgeAnumberwhoseonlyprimefactorsare2,3,5or7iscalledahumblenumber.Thesequence1,2,3,4,5,6,7,8,9,10,12,14,15,16,18,20,21,24,25,27,...showsthefirst20humblenumbers.Writeapro......
  • Go - range
    Arangeloopallowsiteratingoverdifferentdatastructures:StringArrayPointertoanarraySliceMapReceivingchannelIngeneral,rangeproducestwovaluesforeachdatastructure exceptareceivingchannel,forwhichitproducesasingleeleme......
  • cocos create 3.X 控制角色左右上下移动
        代码:/***角色上下左右控制**/import{_decorator,Component,Node,Vec3,Input,EventKeyboard,KeyCode}from'cc';const{ccclass,property}=_decorator;@ccclass('Player')exportclassPlayerextendsComponent{xy:......
  • RangeManager
    \(map\)可用于维护所有标记的数,但只支持单点修改和单点查询,而\(RangeManager\)则能在\(map\)的基础上通过将连续值的标记转换为区间的标记,从而额外支持区间修改和区间查询。\(add\_range(\{l,r\})\):区间修改,标记闭区间\([l,r]\)​的每个数,时间复杂度:\(O(logn)\)。\(re......
  • CF55D Beautiful numbers 题解
    题目链接:CF或者洛谷常见知识点组合经典题。首先,一眼数位dp类型题,考虑需要处理些怎样的判断合法数位信息。经典操作对于跟整除有关的判断,数位dp为了减少使用空间,都可以考虑记忆化模数减少空间开销。对于整除若干个数,即整除这若干个数的最小公倍数即可,是一个非常常用......