Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Builder pattern in Rust

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to achieve the Builder pattern in Rust, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Example

The usual implementation in Rust is to construct the final type by constantly rebuilding the Builder:

Struct Counter {counted1: usize, counted2: usize, done: bool,}

Struct CounterBuilder {counted1: usize, counted2: usize,}

The impl CounterBuilder {/ / builder needs to have a default parameter configuration, which is then triggered to build from the default configuration. / / does not apply to # [derive (std::default::Default)] because the default configuration may be different fn default ()-> Self {CounterBuiler {counted1: 5, counted2: 0,}}

/ / attribute customization method. Consume the original builder, modify the properties and regenerate the new builder fn set_counted1 (self, cnt: usize)-> Self {self.counted1 = cnt; self}

Fn set_counted2 (self, cnt: usize)-> Self {self.counted2 = cnt; self}

/ / finally, the desired type fn build (self) is generated through the `build` method-> Counter {Counter {counted1: self.counted1, counted2: self.counted2, done: false,} personal practice

When setting a property method, the usual implementation is to generate a new constructor by consuming the original constructor, which makes it necessary to recapture the constructor if the process of configuring the constructor cannot call the property setting method continuously:

Let mut builder = CounterBuilder::default ()

/ /... Do some calculations to get the value let cnt1 = operations () that needs to be configured

Builder = builder.set_counted1 (cnt)

/ /... Do some calculations to get the value let cnt2 = operations () that needs to be configured

Builder = builder.set_counted (cnt2)

The above code usually occurs when the flow calculation is needed and the parameter configuration is recorded in a timely manner. And, if the constructor is held by a larger data structure, consuming and rebuilding the constructor may have a slight impact on performance. Therefore, in the personal implementation of bloggers, the method of passing-mut self reference is usually adopted to implement the property setting method:

/ /. / / property customization method. Consume the original builder, modify the properties and regenerate the new builder fn set_counted1 (& mut self, cnt: usize)-> & mut Self {self.counted1 = cnt; self}

Fn set_counted2 (& mut self, cnt: usize)-> & mut Self {self.counted2 = cnt; self}

/ /...

By changing the function signature to the above form, you can flexibly construct the target structure:

Let mut builder = CounterBuilder::default ()

/ /... Do some calculations to get the value let cnt1 = operations () that needs to be configured

Builder.set_counted1 (cnt)

/ /... Do some calculations to get the value let cnt2 = operations () that needs to be configured

Builder.set_counted (cnt2)

/ /... You may have to wait for other operations to complete before building.

Let counter = builder.build (); why the constructor pattern construction process is controllable. Usually when implementing the constructor pattern, we set the properties that the constructor needs to configure to private [^ 1], and can only be set through the property setting method we provide to make the construction process controllable. In addition, you can prevent invalid objects from being generated by early panic (panic) with the property setting method. Set up a single method of responsibility. The property setting method [responsibility specific] is only responsible for setting one property, and the corresponding property setting method needs to be modified only when the setting rule of the attribute is changed; the structure is flexible. Multiple property setting methods can be freely combined or constructed step by step. Can be constructed in batches. In addition to using the consumptive build (self) method, we can also use the non-expendable fn build (& self) method so that the constructor can be reused multiple times. In line with the principle of opening and closing. When the internal implementation of the setting method of a property changes, it does not affect the setting mode of other properties; while when adding properties and their setting methods, you can easily add the settings of new properties through chain calls. Why not use constructor mode

The constructor pattern is not applicable in some scenarios because of the following shortcomings:

The constructed object cannot be used until the construction is complete. The constructor does not generate the constructed object until the construction is complete, so the constructed object cannot be used until the entire construction setup is complete. The constructor uses the same property setting method as the constructed object, resulting in code duplication and non-reuse. Consider that you need to modify the scene of the object only through the property setting method, when the constructed object needs to set properties frequently during use, then you need to write the corresponding property setting method; and if the constructor is also used for object construction, then the property setting method will be repeated, and may cause inconsistent property setting behavior between the constructor and the constructed object [^ 2].

These are all the contents of the article "how to implement the Builder pattern in Rust". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report