That's the point. You don't do this when making a "constructor" in Rust.
Based on the name, "new" should be a constructor but since it takes a mutable reference to self and therefore requires the thing to exist already, it can't be used as a constructor.
So either it's unusable or a case of really bad naming.
Just realised that the title is "Yes, that is supposed to be a constructor". So yeah this code won't work as a constructor since it requires the object to exist.
Yes I know how the self argument works. What I'm saying is the only thing wrong here is the name of the function. Maybe you noticed that op left off the return type? For all we know it's returning the unit type and whoever wrote it is breaking convention and OP misunderstood the function because of the convention breaking name
Breaking convention doesn't necessarily mean the code is bad, it's like saying writing ergonomic bindings in a sys crate is bad. It's not bad it just breaks convention.
Ah I see what you mean, I was just assuming that this is OP's own code and, given the title, they 100% intended it to be a constructor in the manner you would normally see with "new" i.e pub fn new(text: String) -> Self.
If it isn't intended to be a constructor then it's not such a big deal, although I would still argue that the name is a bit confusing given the prevalence of the pattern above.
View all comments
Show parent comment
15
u/thejamibu Dec 03 '22 edited Dec 04 '22
That's the point. You don't do this when making a "constructor" in Rust.
Based on the name, "new" should be a constructor but since it takes a mutable reference to self and therefore requires the thing to exist already, it can't be used as a constructor.
So either it's unusable or a case of really bad naming.