迭代
- use std::collections::HashMap;
- fn main() {
- let mut map=HashMap::new();
- for i in 0..10{
- map.insert(i,i.to_string()+"str");
- }
- for i in map.iter(){
- println!("{:?}",i);
- }
- }
- pub trait GetAdd{
- fn add(&self)->i32;
- fn get_name(&self){
- println!("babay");
- }
- }
- pub trait GetMultiple{
- fn multiple(&self)->i32;
- }
-
- pub struct Point{
- pub x:i32,
- pub y:i32
- }
-
- struct Ractangle{
- lenght:i32,
- width:i32
- }
- impl GetAdd for Point{
- fn add(&self)->i32{
- self.x+self.y
- }
- }
- impl GetMultiple for Point{
- fn multiple(&self)->i32{
- self.x*self.y
- }
- }
- impl GetAdd for Ractangle{
- fn add(&self)->i32{
- self.lenght+self.width
- }
- }
- impl GetMultiple for Ractangle{
- fn multiple(&self)->i32{
- self.width*self.width
- }
- }
-
- fn main(){
- let p1:Point=Point{x:1,y:2};
- let r1:Ractangle=Ractangle{lenght:100,width:200};
- display_add(&p1);
- display_add(&r1);
- let p2:Point=Point{x:2,y:3};
- get_distance(&p1,&p2);
- }
-
- pub fn display_add(arg:&impl GetAdd){
- println!("Point_add={}",arg.add());
- }
-
- pub fn get_distance(p1:&Point,p2:&Point){
- println!("The distance between p1 to p2 is {}",(((p1.x-p2.x).pow(2)+(p1.y-p2.y).pow(2))as f64).sqrt());
- }
- fn main() {
- let arr=[1,2,3,4,5];
- let _v:Vec
=arr.into(); -
- let x=100;
- let _y:f64=x.into();
-
- let s="ssss";
- let _s1:String=s.into();
- }
注意:这个函数的使用对象只能是String或者&str
unwrap 方法,该方法在发现错误时,会直接调用 panic 导致程序的崩溃退出,在实际项目中,请不要这么使用,
- use std::collections::HashMap;
- fn main() {
- let mut map=HashMap::new();
- let text="This is a very nice girl.";
- for word in text.split_whitespace(){
- let count=map.entry(word).or_insert(0);
- *count +=1;
- }
- println!("{:?}",map);
- }
作用:调用panic!宏来终止程序(简而言之:让程序崩溃)。
unwrap 意味着发出以下命令:对Option和Result计算结果值,如果有错误,恐慌并停止程序。
Unwrap 在收到 none 时返回 panic。
- 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!");
- }
增加和删除
匹配字符和删除--trim
trim之后返回&str类型
注意:
format(),to_lowercase(),to_uppercase()返回的是String类型,不是&str.
- fn main() {
- let s:String="abcd".to_string();
- let ups:String=s.to_uppercase();
- println!("s={}\nups={}",s,ups);
-
- let re_s:&str="nnnn";
- let re_up_s:&str=re_s.to_uppercase();//报错
- println!("re_s={}\nre_up_s={}",re_s,re_up_s);
- }
for r in results.lines() {
let v: Vec<&str> = r.split(',').collect();
...
}
string.is_empty()
use std::time::Duration;
时
分:
秒:thread::sleep(Duration::from_secs(1));
求键值对个数:hashmap.len()
求所有键生成vec:let mut keys: Vec<&String> = scores.keys().collect();
求所有值的个数:hashmap.values().sum::
T是interge类型
收集迭代器的结果,生成指定类型的集合,集合的类型要显示的标记出来。
- fn main() {
- let a=[1,2,3,4,5];
- let v:Vec<_>=a.iter().map(|x|x*2).collect();
- println!("{:?}",v);
- }
一定要使用into_iter()
- use std::collections::HashMap;
- fn main() {
- let a=[(1,"n1"),(2,"n2"),(3,"n3")];
- let hmap:HashMap<_,_>=a.into_iter().collect();
- println!("{:?}",hmap);
- println!("{:?}",a);//静态数组,不会被move
- }
筛选出符合条件的元素
- fn main() {
- let a=[1,2,3,4,5];
- let v:Vec<_>=a.iter().filter(|x|*x%2==0).collect();
- println!("{:?}",a);
- println!("{:?}",v);
- }

拿走一个变量合作一个值,可以返回这个变量的值或者返回给定的常量值。可以是数组。