首页 > 其他分享 >[Rust] Collect()

[Rust] Collect()

时间:2023-05-15 15:13:04浏览次数:32  
标签:i32 iter Collect let vec println foo Rust

String collect: automaticlly calling concat on string

    let foo: String = vec!["this", "is", "a", "test"]
        .into_iter()
        .collect();
    println!("{:?}", foo); // thisisatest

 

HashSet:

let foo: HashSet<isize> = vec![1, 2, 3]
    .into_iter()
    .collect();

 

HashMap:

use std::collections::HashMap;

fn main() {
    let foo: HashMap<&str, usize> = vec!["this", "is", "a", "test"]
        .into_iter()
        .enumerate()
        .map(|(idx, item)| (item, idx))
        .collect();
    println!("{:?}", foo); // {"this": 0, "is": 1, "a": 2, "test": 3}
}

 

Number:

let value: usize = vev![1,2,3]
    .iter()
    .sum(); // 6

 

let how_many_items: usize = vec![1,2,3]
    .iter()
    .skip(2)
    .count(); // 1

 

vec![1,2,5,9,4]
    .iter()
    .skip(2)
    .take_while(|&x| x > 4)
    .for_each(|&x| println!("{}", x)) // 5, 9

 

let what_about_this: usize = vec![1, 2, 3]
    .iter()
    .filter(|x| *x % 2 == 0)
    .count() // 1

In Rust, the .iter() method on a Vec<T> creates an iterator over immutable references to the elements of the vector. Therefore, in the context of your filter function, x is of type &i32, not i32.

The * operator is used to dereference x to get the value it points to. Without it, you'd be trying to perform the modulus operation on a reference (&i32), not the integer value (i32) itself. The Rust compiler will not automatically dereference the value for you in this context, so you need to use the * operator to explicitly dereference it.

 

let map = HashMap::from([
   ("foo", 1),
   ("bar", 2),
   ("baz", 3),
]);

map
    .iter()
    .for_each(|(k, v)| println!("{}: {}", k, v));




let set = HashSet::from([
    "foo",
    "bar",
    "baz",
]);

set
    .iter()
    .for_each(|v| println!("{}", v));

 

标签:i32,iter,Collect,let,vec,println,foo,Rust
From: https://www.cnblogs.com/Answer1215/p/17401931.html

相关文章

  • 【计算几何】Rust求解平面最近点对(寻找距离最近的两个点的距离)
    目录题目地址代码题目地址https://ac.nowcoder.com/acm/contest/52826/C代码usestd::io;usestd::cmp::Ordering;usestd::f64;#[derive(Debug,PartialEq,PartialOrd,Clone,Copy)]structPoint{x:f64,y:f64,}fneuclidean_distance(p1:&Point,p2:......
  • MongoDB 功能详解之时间序列集合(Time Series Collections)
    MongoDB功能详解之时间序列集合(TimeSeriesCollections)      时间序列集合(TimeSeriesCollections):MongoDB5.0版本中的新功能。时间序列数据是一系列数据点,通过分析这些随时间变化的数据点而获得对数据的深刻理解。时间序列数据通常由以下组成部分组成:时间:数......
  • collection.abc模块下的抽象基类UML类图说明
    说明Iterable、Container和Sized每个容器都应该继承这三个抽象基类,或者实现兼容的协议。Iterable通过__iter__方法支持迭代,Container通过__contains__方法支持in运算符,Sized通过__len__方法支持len()函数。Collection这个抽象基类是3.6新增的,自身没有方法,目的是方便子类化I......
  • Rust 笔记
    TheRustProgrammingLanguageRust编程语言笔记。来源:TheRustProgrammingLanguageBook。安装使用rustup来安装Rust。Rust源文件以.rs作为扩展名。几个常用的命令:编译:rustc.rs-file运行:./compiled-file检查Rust编译器版本:rustc--version检查rustup......
  • 文盘Rust —— rust连接oss | 京东云技术团队
    作者:京东科技贾世闻对象存储是云的基础组件之一,各大云厂商都有相关产品。这里跟大家介绍一下rust与对象存储交到的基本套路和其中的一些技巧。基本连接我们以[S3sdk](https://github.com/awslabs/aws-sdk-rust)为例来说说基本的连接与操作,作者验证过aws、京东云、阿里云。......
  • RustDesk 远程桌面
    RustDesk是一款开源远程桌面软件。有云服务器的话,可以几分钟就搭一个,本文是搭建的记录。自建服务器下载服务器程序,#上传进服务器,假设其IP为`x.x.x.x`[email protected]:登录进服务器:#解压unziprustdesk-server-linux-amd64.zip#......
  • Java-Day-17( 集合( Collection 里的 List、Set ) )
    Java-Day-17集合先前用于保存多个数据使用的是——数组长度开始必须指定,且不能更改保存的必须为同一类型的元素使用数组进行增删元素的代码较为麻烦例:扩容的要先建新数组,再拷贝原数据、添加新对象引出集合可以动态保存任意多个对象,使用比较方便提供了一系列......
  • [记]Rust使用winrt库调用第三方C# DLL库的方法?
    Rust是一门系统编程语言,它的运行时比较"轻量级",因此难以跨平台地直接调用WindowsRuntime(WinRT)组件。不过我们可以通过Rust库winrt来操作WinRT组件,同时也可以通过Rust的FFI(ForeignFunctionInterface)功能来调用第三方C#DLL库。以下是调用第三方C#DLL......
  • 关于ObservableCollection的更新与不更新分析
    因为最近在WPF项目中,遇到ObservableCollection这个属性的频繁使用,一个一个坑跳过来,今天看到这个贴子玩转INotifyPropertyChanged和ObservableCollection-包建强-博客园(cnblogs.com)其中分析很透彻了,但是留了一点遗憾,而且在其中引起了一个想法,做一个项目来测试一下。我们......
  • collection接口
    Collection接口和常用方法collection接口包含:list和set两个接口而list里有Vector类,ArrayList类和LinkedList类set里有:Hashset类和Treeset类collection接口实现类的特点collection类实现子类可以存放多个元素,每个元素可以是Object有些Collection的实现类,可以存放重复的元素......