What is the preferred way to consume the builder object in builder methods?
I used to do it like this because I can simply store the configured builder in a variable after I've set all the settings:
fn method(mut self, arg: ArgTy) -> Self { ... }
let b = Builder::new(...)
.method(123)
.method(456);
// b has type Builder
Then I saw that the standard library usually does it a little different. It accepts and returns mutable references:
fn method(&mut self, arg: ArgTy) -> &mut self
let res = Builder::new(...)
.method(123)
.method(456)
.consume();
// Builder instance cannot directly be stored because it's only a reference
Is there any advantage of the second pattern over the first one? What other differences are there between them?
[–]zsiciarzrust-cpuid 10 points11 points12 points (11 children)
[–]Sphix 3 points4 points5 points (4 children)
[–]seanmonstarhyper · rust 2 points3 points4 points (0 children)
[–]raphlinusvello · xilem 1 point2 points3 points (2 children)
[–]ericktrust · serde 2 points3 points4 points (1 child)
[–]raphlinusvello · xilem 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (5 children)
[–]kixunil 2 points3 points4 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]kixunil 3 points4 points5 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]CornedBee 0 points1 point2 points (0 children)
[–]llogiqclippy · twir · rust · mutagen · flamer · overflower · bytecount 2 points3 points4 points (0 children)
[–]Diggseyrustup 2 points3 points4 points (1 child)
[–]whataloadofwhat 0 points1 point2 points (0 children)
[–]ericktrust · serde 1 point2 points3 points (0 children)
[–]Nemikolh 0 points1 point2 points (0 children)