rust

Safest way to make a serde_json result globally avaialable [duplicate]

痴心易碎 提交于 2020-12-26 05:19:18
问题 This question already has answers here : How do I create a global, mutable singleton? (3 answers) Is it possible to use global variables in Rust? (5 answers) Closed 18 days ago . What is a safe way to make a serde_json result globally available in rust? For instance I have a pub function in a module that contains let contents = fs::read_to_string(json_file_path).expect("file not found"); let myVar: Foo = match serde_json::from_str(&contents){ ...} println!("WIDTH:{}", myVar.Output.width);

Disable default constructor in Rust?

巧了我就是萌 提交于 2020-12-26 05:19:11
问题 Say I define my own type in a Rust library, like so: struct Date { year: u16, month: u8, day: u8 } impl Date { fn new(y: u16, m: u8, d: u8) -> Date { // Do some validation here first Date { year: y, month: m, day: d } } } Is there a way to require users to use the Date::new constructor? In other words, can I somehow prohibit users from creating their own Date struct with the default constructor like so: let d = Date { 2017, 7, 10 }; I ask because it seems to be a detrimental flaw if you

Lifetime issue when using the Any trait to get references to structs containing references

谁都会走 提交于 2020-12-26 04:28:26
问题 I ran into a lifetime problem with a little game. The below code represents a very boiled down version of the update loop. I need the container mutable reference to get references to other game objects or to create new ones or trigger a functionality. For that reason, I need the Any trait to be able to cast the trait to a struct, so in my GameObj trait I added an as_any method, but this resulted in a lifetime issue. use std::any::Any; trait GameObj<'a> { fn as_any<'b>(&'b self) -> &'b (dyn

Lifetime issue when using the Any trait to get references to structs containing references

房东的猫 提交于 2020-12-26 04:27:59
问题 I ran into a lifetime problem with a little game. The below code represents a very boiled down version of the update loop. I need the container mutable reference to get references to other game objects or to create new ones or trigger a functionality. For that reason, I need the Any trait to be able to cast the trait to a struct, so in my GameObj trait I added an as_any method, but this resulted in a lifetime issue. use std::any::Any; trait GameObj<'a> { fn as_any<'b>(&'b self) -> &'b (dyn

What does it mean to instantiate a Rust generic with an underscore?

北慕城南 提交于 2020-12-25 08:27:30
问题 While working with serde_json for reading json documents, I wrote the following line of code to obtain the result of unwrapping the return value of serde_json::from_str : fn get_json_content(content_s: &str) -> Option<Value> { let ms: String = serde_json::from_str(content_s).unwrap; // <-- match serde_json::from_str(content_s) { Ok(some_value) => Some(some_value), Err(_) => None } } As you can see, I forgot the () on the end of the call to unwrap , which resulted in the following error: error

What does it mean to instantiate a Rust generic with an underscore?

空扰寡人 提交于 2020-12-25 08:25:47
问题 While working with serde_json for reading json documents, I wrote the following line of code to obtain the result of unwrapping the return value of serde_json::from_str : fn get_json_content(content_s: &str) -> Option<Value> { let ms: String = serde_json::from_str(content_s).unwrap; // <-- match serde_json::from_str(content_s) { Ok(some_value) => Some(some_value), Err(_) => None } } As you can see, I forgot the () on the end of the call to unwrap , which resulted in the following error: error

Generic implementation depending on traits

假如想象 提交于 2020-12-23 11:50:48
问题 When defining a generic struct , is there a way in Rust to use different implementation of a method according to which trait is implemented by the given generic type T ? For example: struct S<T> { value: T, } impl<T> S<T> { fn print_me(&self) { println!("I cannot be printed"); } } impl<T: std::fmt::Display> S<T> { fn print_me(&self) { println!("{}", self.value); } } fn main() { let s = S { value: 2 }; s.print_me(); } 回答1: There is an unstable feature known as specialization which permits

Generic implementation depending on traits

北城余情 提交于 2020-12-23 11:50:26
问题 When defining a generic struct , is there a way in Rust to use different implementation of a method according to which trait is implemented by the given generic type T ? For example: struct S<T> { value: T, } impl<T> S<T> { fn print_me(&self) { println!("I cannot be printed"); } } impl<T: std::fmt::Display> S<T> { fn print_me(&self) { println!("{}", self.value); } } fn main() { let s = S { value: 2 }; s.print_me(); } 回答1: There is an unstable feature known as specialization which permits

据传苹果汽车将会提前问世,明年 Q3 对外发布

孤人 提交于 2020-12-23 10:35:00
据传苹果汽车将会提前问世,明年 Q3 对外发布 据外媒报道,援引未透露姓名的中国台湾制造商高管的话说,苹果传闻已久的电动汽车将至少提前两年推出,预计会在 2021 年第三季度发布。该报道称,台湾制造商们正在准备最早在明年第二季度增加“苹果汽车”零部件的生产,另外还补充称,苹果已经在加州的道路上秘密测试了数十辆原型车。 来源: cnbeta 拍一拍:不知道苹果电动车会是什么体验,也会有个 AppStore 吗? Rust 语言正吸引更多项目使用 Rust 语言项目公布的最新调查显示,83% 的回应者表示正在使用 Rust 从事开发。Rust 是由 Mozilla 创造的语言,Mozilla 用它开发了实验性的渲染引擎 Servo,并逐渐将部分组件整合到 Firefox 中,但在前不久的裁员中 Mozilla 裁掉了许多 Rust 项目的核心开发者。Rust 项目宣布独立,而 Servo 项目则成为了 Linux 基金会托管项目。微软和亚马逊 AWS 部门的工程师都在尝试使用 Rust 项目开发更安全的软件。 来源: solidot 拍一拍:RUST 这些年已经炙手可热了,想必能从 C/C++ 和 Java 手中抢下一些份额。 黑客免费公布 27 万加密货币钱包 Ledger 用户信息 近日,一名网络攻击者在黑客论坛发帖,免费公布了从 Ledger 窃取的电子邮件和邮寄地址。在今年 6

How to wrap a borrowed value in a newtype that is also a borrowed value?

。_饼干妹妹 提交于 2020-12-21 02:54:45
问题 I am trying to use the newtype pattern to wrap a pre-existing type. That inner type has a modify method which lets us work with a borrowed mutable value in a callback: struct Val; struct Inner(Val); impl Inner { fn modify<F>(&self, f: F) where F: FnOnce(&mut Val) -> &mut Val { … } } Now I want to provide a very similar method on my newtype Outer , which however should not work on Val s but again a newtype wrapper WrappedVal : struct Outer(Inner); struct WrappedVal(Val); impl Outer { fn modify