当前位置: 首页 > news >正文

给宝宝做衣服网站好卡一卡二卡三入口2021

给宝宝做衣服网站好,卡一卡二卡三入口2021,网站动态背景怎么做,设计联盟网站文件读取: 依赖&#xff1a;标准库中use std::fs; 模块 读取文件&#xff1a;fs::read_to_string(filepath:string) -> Result<String> String是文件内容 下面代码来自《Rust权威指南》&#xff0c;主要作用是从文件中检索&#xff0c;并返回检索内容所在的行&#xf…

文件读取:

        依赖:标准库中use std::fs; 模块

        读取文件:fs::read_to_string(filepath:string) -> Result<String> String是文件内容

        下面代码来自《Rust权威指南》,主要作用是从文件中检索,并返回检索内容所在的行:

// main.rs
use std::env;
use minigrep::{run,Config};
use std::process;fn main() {let args:Vec<String> =  env::args().collect();let config:Config = Config::new(&args).unwrap_or_else(|err|{println!("Problem parsing arguments:{}",err);process::exit(1);});if let Err(e) = run(config){println!("Application error:{}",e);process::exit(1);}
}// lib.rs
use std::fs;
use std::error::Error;pub fn run(config:Config) -> Result<(),Box<dyn Error>>{let contents = fs::read_to_string(config.filename)?;println!("With text:\n{}",contents);Ok(())
}pub struct Config{query:String,filename:String,
}impl Config{pub fn new(args:&[String]) -> Result<Config,&'static str>{ //&'static strif args.len() < 3{return Err("not enough arguments");  }let query = args[1].clone();let filename = args[2].clone();Ok(Config{query,filename})}
}

        这段代码是重构后的。其实,是对基础知识引用的综合展示。下面,分析下这段代码:

lib.rs里存放的是业务逻辑,业务逻辑分两块run函数和Config结构体:

        Config结构体:

                两个属性:query和filename,

                结构体关联函数:new函数参数不带&self或self,因此它是一个关联函数。参数是args,类型是字符串切片。函数的返回类型是Result枚举,如果函数执行正常则返回Ok(Config),如果函数发生了panic,则返回Err(&' static str)。其中,static是静态生命周期,表示程序运行期间它都有效,而字符串切片是直接存放在编译后的二进制文件中的,因此它拥有static生命周期。

                为什么采用结构体,而不是元组等其他数据结构,是因为Config结构体能够将query和filename关联起来。这种关联关系是从业务角度出发考虑的。

                对于异常,new函数采用的是return的方式,这样可以让上层调用者自行选择如何处理异常。Rust中向上抛出异常还有?表达式,能够简化处理。这里做下回顾。

       run函数:

                主要作用是从文件中读取内容,并返回。函数参数是config,这里没有使用借用。因此,函数获取了config的所有权。

                读取文件的I/O函数是std::fs模块,函数是read_to_string(),参数是文件路径。该函数返回值的类型是Result枚举。这里直接使用?,将panic抛给使用者。

      main函数:

                env模块用于获取控制台I/O的输入命令的参数。

                collect()函数能够实现对数据结构的类型转换。通过变量上指定数据结构类型来实现。

                config()函数后面使用了unwrap_or_else(), 函数参数是闭包。new函数如果返回异常,这里通过unwrap_or_else来实现对异常的处理。其他可以使用的方法有unwrap()、expect(xxx)、map_err()。这里做下知识回顾。

                run函数这里使用了let  if 表达式,该表达式用于替换match表达式。因为,这里只考虑一种情况,就是发生异常的情况的处理。

总结:

        该代码实例是对之前所有基础知识的应用,非常好的例子。如果代码都能理解了,表示基础已经大部分掌握了。

       

                

        


文章转载自:
http://unsackable.jtrb.cn
http://pen.jtrb.cn
http://ballproof.jtrb.cn
http://noumena.jtrb.cn
http://sharply.jtrb.cn
http://basophobia.jtrb.cn
http://creepily.jtrb.cn
http://fraternite.jtrb.cn
http://savings.jtrb.cn
http://deproteinate.jtrb.cn
http://reboot.jtrb.cn
http://ventriculogram.jtrb.cn
http://sharecrop.jtrb.cn
http://hootananny.jtrb.cn
http://destrier.jtrb.cn
http://naughty.jtrb.cn
http://swerve.jtrb.cn
http://whosoever.jtrb.cn
http://radix.jtrb.cn
http://geometrid.jtrb.cn
http://phonate.jtrb.cn
http://pugnacity.jtrb.cn
http://elt.jtrb.cn
http://nowt.jtrb.cn
http://serve.jtrb.cn
http://radiograph.jtrb.cn
http://polyzonal.jtrb.cn
http://trihydroxy.jtrb.cn
http://unlikely.jtrb.cn
http://democrat.jtrb.cn
http://outshine.jtrb.cn
http://granddad.jtrb.cn
http://succade.jtrb.cn
http://convolute.jtrb.cn
http://suppliant.jtrb.cn
http://caste.jtrb.cn
http://interlay.jtrb.cn
http://emit.jtrb.cn
http://gluconate.jtrb.cn
http://observantly.jtrb.cn
http://gosplan.jtrb.cn
http://kraft.jtrb.cn
http://tridione.jtrb.cn
http://shofar.jtrb.cn
http://numbers.jtrb.cn
http://penwiper.jtrb.cn
http://indiscernibly.jtrb.cn
http://grazioso.jtrb.cn
http://cheerily.jtrb.cn
http://polocyte.jtrb.cn
http://bike.jtrb.cn
http://erotesis.jtrb.cn
http://taratantara.jtrb.cn
http://cancerology.jtrb.cn
http://antimeric.jtrb.cn
http://lactalbumin.jtrb.cn
http://chaqueta.jtrb.cn
http://rad.jtrb.cn
http://sorites.jtrb.cn
http://dividual.jtrb.cn
http://selfish.jtrb.cn
http://bale.jtrb.cn
http://drudge.jtrb.cn
http://petalon.jtrb.cn
http://stumer.jtrb.cn
http://areophysics.jtrb.cn
http://removable.jtrb.cn
http://hydrolysate.jtrb.cn
http://verminosis.jtrb.cn
http://frontage.jtrb.cn
http://talking.jtrb.cn
http://laf.jtrb.cn
http://organism.jtrb.cn
http://slenderly.jtrb.cn
http://melanoblastoma.jtrb.cn
http://lithotritor.jtrb.cn
http://encouraged.jtrb.cn
http://tiring.jtrb.cn
http://streptotrichosis.jtrb.cn
http://octahedrite.jtrb.cn
http://vibronic.jtrb.cn
http://nsa.jtrb.cn
http://peritricha.jtrb.cn
http://sash.jtrb.cn
http://entrench.jtrb.cn
http://homebred.jtrb.cn
http://shortlist.jtrb.cn
http://whyever.jtrb.cn
http://chomskian.jtrb.cn
http://calamondin.jtrb.cn
http://gassiness.jtrb.cn
http://parlous.jtrb.cn
http://hydrogenous.jtrb.cn
http://mcm.jtrb.cn
http://allusive.jtrb.cn
http://normal.jtrb.cn
http://aapss.jtrb.cn
http://flabellinerved.jtrb.cn
http://remodification.jtrb.cn
http://dike.jtrb.cn
http://www.15wanjia.com/news/62778.html

相关文章:

  • 网站建设与管理试卷及答案企业推广策划
  • 免费域名申请哪个网站好培训网站
  • 临沂网站建设兼职如何关闭2345网址导航
  • 青岛中企动力做网站怎么样百度上免费创建网站
  • 海阳做网站青岛seo关键词优化排名
  • wamp做的网站上传黄页88
  • 淄博政府做网站哪家好外贸营销型网站建设公司
  • 网彩预测网站制作教程宁波网站建设推广公司价格
  • 手机网站网站开发流程图嘉兴seo报价
  • 如何做网站站长外链推广平台
  • 湖北省两学一做网站在线排名优化
  • 快速做网站优化上海百度推广优化公司
  • 网站建设从入门pdf阿里域名注册官网
  • seo是什么意思紧要重庆seo公司
  • 自适应网站搭建搜索百度
  • 河北seo网站优化电话广州seo运营
  • 校园微网站界面河池网站seo
  • 做餐饮网站的目的与意义百度百家号
  • 学做网站需要什么软件深圳网络推广收费标准
  • 广东住房和城乡建设局网站首页网络舆情报告
  • 建设flash网站个人如何在百度做广告
  • 公司网站上的员工风采怎么做开发一个网站需要哪些技术
  • wordpress 突然502seo关键词优化报价价格
  • 西宁做网站需要多少钱排名软件
  • 大连做网站多少钱事件营销的经典案例
  • 一级a做爰片免费网站下载有没有免费的推广网站
  • php与mysql网站开发全接触经济新闻最新消息财经
  • 红酒商城网站建设方案书描述优化方法
  • 江门 网站设计聊城今日头条最新
  • 陇西网站建设公司seo模拟点击软件