rust

go 和 rust 设计模式对比

倾然丶 夕夏残阳落幕 提交于 2021-02-16 19:06:45
定义 工厂模式可分为三类: 简单工厂模式 工厂方法模式 抽象工厂模式 简单工厂模式 属于创建型模式,不属于 23 种 GOF 设计模式之一,由一个工厂对象决定创建出哪一种产品类实例,逻辑为定义一个工厂类,根据传入的参数不同返回不同的产品实例,被创建的实例具有共同的父类或接口。 工厂方法模式 跟简单工厂模式的区别就是,定义了一些工厂子类,每个工厂子类都可以创建相对应的产品类实例,而不用工厂父类去根据参数创建,一般在产品类比较多的情况下,不想太多复杂的逻辑封装在工厂父类里面,可以用这种模式。 抽象工厂模式 顾名思义,把工厂抽象出来,根据具体的业务需求组合工厂创建产品实例的逻辑,通常为创建多个产品实例类。抽象工厂一旦要增加创建产品实例的逻辑,就需要修改抽象工厂接口的具体产品组合逻辑,所以并不符合开闭原则,如果是需要频繁增加(修改)产品实例的话,要衡量是否采用这种模式 基本结构 工厂模式从源码上总的来说可以分为 4 部分: 抽象产品: 通过声明接口(特性),定义了产品的业务方法。 具体产品: 通过 implement 接口(特性),定义了业务方法的具体逻辑。 抽象工厂: 按照相应的业务逻辑,声明了创建产品对象的业务方法(多数为组合方式)。 具体工厂: 通过 implement 接口(特性),定义了创建产品对象的具体逻辑。 结构设计的关键点: 产品只负责产品对象的创建,其他什么都不关心,

How to efficiently push displayable item into String? [duplicate]

折月煮酒 提交于 2021-02-16 18:54:25
问题 This question already has an answer here : How can I append a formatted string to an existing String? (1 answer) Closed 3 years ago . See this example: fn concat<T: std::fmt::Display>(s: &mut String, thing: T) { // TODO } fn main() { let mut s = "Hello ".into(); concat(&mut s, 42); assert_eq!(&s, "Hello 42"); } I know that I can use this: s.push_str(&format!("{}", thing)) but this is not the most efficient, because format! allocate a String that is not necessary. The most efficient is to

Capture all Regex matches into a vector

断了今生、忘了曾经 提交于 2021-02-16 16:19:26
问题 I'd like to capture all the numbers in a string and return a vector of integers, something like this (the result can be an empty vector): fn str_strip_numbers(s: &str) -> Vec<isize> { unimplemented!() } A Python prototype: def str_strip_numbers(s): """ Returns a vector of integer numbers embedded in a string argument. """ return [int(x) for x in re.compile('\d+').findall(s)] For "alfa" the result is [] , for "42by4" it is [42, 4] . What is the idiomatic way to get it in Rust? UPD: fn str

How to return concrete type from generic function?

天大地大妈咪最大 提交于 2021-02-16 15:29:06
问题 In the example below the Default trait is used just for demonstration purposes. My questions are: What is the difference between the declarations of f() and g() ? Why g() doesn't compile since it's identical to f() ? How can I return a concrete type out of a impl trait generically typed declaration? struct Something { } impl Default for Something { fn default() -> Self { Something{} } } // This compiles. pub fn f() -> impl Default { Something{} } // This doesn't. pub fn g<T: Default>() -> T {

Rust serde deserializing a mixed array

北慕城南 提交于 2021-02-16 15:10:26
问题 In Rust i am receiving data from a websocket. For simplicity it looks like this: [1, {"a": ["1.2345", 5, "9.8765"]}] The string i get from the websocket is indeed double-quoted 'floating point values' (thus in actuality strings), and unquoted integers. I want to deserialize this object into a struct. But since the return array "a" is of mixed type, I can't use something like: struct MyStruct { id: i32, a: [f64; 3], } So I thought let's define another struct: struct Ask { price: f64, whole_lot

How to run multiple futures that call thread::sleep in parallel? [duplicate]

帅比萌擦擦* 提交于 2021-02-16 13:15:32
问题 This question already has answers here : Why does Future::select choose the future with a longer sleep period first? (1 answer) What is the best approach to encapsulate blocking I/O in future-rs? (1 answer) Closed 2 years ago . I have a slow future that blocks for 1 second before running to completion. I've tried to use the join combinator but the composite future my_app executes the futures sequentially: #![feature(pin, futures_api, arbitrary_self_types)] extern crate futures; // v0.3 use

“invalid type: map, expected a sequence” when deserializing a nested JSON structure with Serde

点点圈 提交于 2021-02-16 10:49:30
问题 I am trying to poll the GitHub API for issues and print them out. To do so, I need to deserialize a nested JSON structure that I receive from a cURL GET request. I am trying to get the url for all the objects in the items array: { "total_count": 4905, "incomplete_results": false, "items": [ { "url": "https://api.github.com/repos/servo/saltfs/issues/789", "repository_url": "https://api.github.com/repos/servo/saltfs", "labels_url": "https://api.github.com/repos/servo/saltfs/issues/789/labels{

“invalid type: map, expected a sequence” when deserializing a nested JSON structure with Serde

左心房为你撑大大i 提交于 2021-02-16 10:48:10
问题 I am trying to poll the GitHub API for issues and print them out. To do so, I need to deserialize a nested JSON structure that I receive from a cURL GET request. I am trying to get the url for all the objects in the items array: { "total_count": 4905, "incomplete_results": false, "items": [ { "url": "https://api.github.com/repos/servo/saltfs/issues/789", "repository_url": "https://api.github.com/repos/servo/saltfs", "labels_url": "https://api.github.com/repos/servo/saltfs/issues/789/labels{

Does rustc / cargo have a -march=native equivalent?

99封情书 提交于 2021-02-16 10:21:28
问题 I feel like binary portability isn't really a concern, and so something like -march=native may always be the default behavior. I can't find anything saying one way or the other though. 回答1: As mentioned in the comments, pass the -C target-cpu option to rustc: rustc -C target-cpu=native For more options: $ rustc -C help ... -C target-cpu=val -- select target processor (rustc --print target-cpus for details) ... See How to pass rustc flags to cargo? for more methods of passing the option. I

Is it possible to modify the case of a token inside of a macro?

前提是你 提交于 2021-02-15 06:01:20
问题 I am writing a macro which creates a struct managing user input. I am using the crates bitflags and sdl2. Return is an example for the key Return . This macro takes a list of all possible inputs and then ($($flag:ident = $value:expr;)+) => { ... } Creates a new bitflag with the name of the input bitflags!( struct KeyType: u64 { $( const $flag = $value;// UPPER_CASE is the norm for globals: 'RETURN' )+ } ); Checks if the key is pressed, using the Keycode enum. match event { $(Event::KeyDown {