rust

Why is `ref` used instead of an asterisk in pattern matching?

拥有回忆 提交于 2021-01-07 02:52:32
问题 I am having trouble trying to understand pattern matching rules in Rust. I originally thought that the idea behind patterns are to match the left-hand side and right-hand side like so: struct S { x: i32, y: (i32, i32) } let S { x: a, y: (b, c) } = S { x: 1, y: (2, 3) }; // `a` matches `1`, `(b, c)` matches `(2, 3)` However, when we want to bind a reference to a value on the right-hand side, we need to use the ref keyword. let &(ref a, ref b) = &(3, 4); This feels rather inconsistent. Why can

Why is `ref` used instead of an asterisk in pattern matching?

筅森魡賤 提交于 2021-01-07 02:52:25
问题 I am having trouble trying to understand pattern matching rules in Rust. I originally thought that the idea behind patterns are to match the left-hand side and right-hand side like so: struct S { x: i32, y: (i32, i32) } let S { x: a, y: (b, c) } = S { x: 1, y: (2, 3) }; // `a` matches `1`, `(b, c)` matches `(2, 3)` However, when we want to bind a reference to a value on the right-hand side, we need to use the ref keyword. let &(ref a, ref b) = &(3, 4); This feels rather inconsistent. Why can

How to set desired return type in match structure?

帅比萌擦擦* 提交于 2021-01-07 02:09:34
问题 In the example in the crate documentation of serde_json (parse JSON into a Rust struct), error handling is omitted: use serde::{Deserialize, Serialize}; use serde_json::Result; #[derive(Serialize, Deserialize)] struct Person { name: String, age: u8, phones: Vec<String>, } fn typed_example() -> Result<()> { // Some JSON input data as a &str. Maybe this comes from the user. let data = r#" { "name": "John Doe", "age": 43, "phones": [ "+44 1234567", "+44 2345678" ] }"#; // Parse the string of

Rust编程进阶:015、方法中的生命周期

巧了我就是萌 提交于 2021-01-05 23:34:17
struct StuA<'a> { name: &'a str, } impl<'b> StuA<'b> { fn do_something(&self) -> i32 { 3 } fn do_something2(&self, s: &str) -> &str { // fn do_something2<'b>(&'b self, s: &str) -> &'b str{ self.name } fn do_something3<'a>(&self, s: &'a str) -> &'a str { s } } fn main() { let s = String::from("hello"); let a = StuA { name: &s }; println!("{}", a.do_something()); let s2 = String::from("hello"); println!("{}", a.do_something2(&s2)); println!("{}", a.do_something3(&s2)); println!("Hello, world!"); } 本节全部源代码: https://github.com/anonymousGiga/learn_rust/blob/master/learn_life4/src/main.rs 来源:

2021全国大学生计算机系统能力大赛操作系统设计赛第一场研讨会隆重举行

喜欢而已 提交于 2021-01-05 09:49:53
为吸引全国高校学生对操作系统的关注与兴趣,培养我国急需的操作系统研发人才,助力国产操作系统的发展,全国高等学校计算机教育研究会、系统能力培养研究专家组、系统能力培养研究项目发起高校决定主办 2021年全国大学生计算机系统能力大赛操作系统设计赛 (以下简称“OS比赛”)。12月26日,本次比赛的第一场研讨会在中科院计算所隆重举行,会议邀请了十余位来自学术界、产业界的专家和优秀的大学生对操作系统开源成果进行分享与交流。 本届OS比赛和研讨会得到了麒麟软件、翼辉信息、蚂蚁集团、国科环宇、360、 匿名捐赠者、OpenAnolis社区、FlyAI算法竞赛社区、全志科技、元心科技、中科创达、龙芯中科、睿赛德rtthread的大力支持。 OS比赛,即将打响! 本次研讨会由清华大学陈渝和向勇老师主持。中科院计算机所研究员包云岗在欢迎辞中表示:“操作系统中重要的部分是将社区建立起来,而构建社区有两大关键词,一是 开源 ,作为一种推动创新的重要方式,OS比赛中,无论是硬件还是软件,都将以开源方式推进。二是 和而不同 ,OS比赛并非仅仅是争夺名次,而是面向所有对OS设计感兴趣的学生,帮助其提升操作系统方面的知识和能力。在此,也鼓励大家积极参与OS比赛。” 北京大学教授陈向群在致辞中提到,人才培养已经从知识为本转向能力为本,而系统能力是计算机领域学生最重要的能力

How to automatically expose all .rs files in a directory in a module?

萝らか妹 提交于 2021-01-05 07:51:04
问题 I have module files generated from protobuf definitions. There are a lot of files and they will grow with time. This is the structure: proto_rust/src/lib.rs proto_rust/src/protos/{lots of auto generated .rs files} proto_rust/src/protos/mod.rs Since there are lots of files in proto_rust/src/protos/ , it does not make sense for me to manually put them in mod.rs . Is there a way to expose all them to lib.rs ? Something like pub mod * . 回答1: Use dtolnay's automod crate. automod::dir!("path/to

Why does “break” not need a semicolon when ending a “loop”?

£可爱£侵袭症+ 提交于 2021-01-05 05:56:31
问题 Excerpt from Chapter 3.5 of the Rust Book: ... we use the break keyword with the value counter * 2 . After the loop, we use a semicolon to end the statement that assigns the value to result . Plus the code snippet: fn main() { let mut counter = 0; let result = loop { counter += 1; if counter == 10 { break counter * 2; } }; println!("The result is {}", result); } I understand how this works and why the result is 20, but I noticed that if I remove the semicolon on the line that contains the

Why does “break” not need a semicolon when ending a “loop”?

半世苍凉 提交于 2021-01-05 05:51:28
问题 Excerpt from Chapter 3.5 of the Rust Book: ... we use the break keyword with the value counter * 2 . After the loop, we use a semicolon to end the statement that assigns the value to result . Plus the code snippet: fn main() { let mut counter = 0; let result = loop { counter += 1; if counter == 10 { break counter * 2; } }; println!("The result is {}", result); } I understand how this works and why the result is 20, but I noticed that if I remove the semicolon on the line that contains the

Why does “break” not need a semicolon when ending a “loop”?

淺唱寂寞╮ 提交于 2021-01-05 05:51:26
问题 Excerpt from Chapter 3.5 of the Rust Book: ... we use the break keyword with the value counter * 2 . After the loop, we use a semicolon to end the statement that assigns the value to result . Plus the code snippet: fn main() { let mut counter = 0; let result = loop { counter += 1; if counter == 10 { break counter * 2; } }; println!("The result is {}", result); } I understand how this works and why the result is 20, but I noticed that if I remove the semicolon on the line that contains the

Rust: tokio,异步代码与运行速度初探

邮差的信 提交于 2021-01-04 14:10:41
几个问题: 1、同样的一段代码,放在异步中,运行速度会如何? 2、什么情况下异步并没有改变运行速度? 3、如何提升异步的速度? toml: [dependencies] tokio = { version = "1", features = ["full"] } futures = "0.3.4" 代码: use std::time::{Duration, Instant}; use std::thread; use tokio::time; const N:i32 =1000000; struct bar{ price :f64, code: Option<String>, close: f64, open:f64, high:f64, low:f64, } impl bar{ fn default()->Self{ bar{ price:0.0, code :Some(String::from("I AM NULL")), close:0.0, open:0.0, high:0.0, low:0.0, } } fn push_vec(n:i32)->Vec<Self>{ let mut v = vec![]; for _ in 0..N{ v.push(bar::default()); } v } } fn hello(){ let v = bar::push_vec(N);