3 ms·
A trivial example is simply a struct type and its "impl". You're already repeating yourself there, but if you want a more concrete example, have a look at what
by jiggawatts 5y ago
A trivial example is simply a struct type and its "impl". You're already repeating yourself there, but if you want a more concrete example, have a look at what it takes to refactor even trivial pieces of code in Rust: https://tinyurl.com/56zny85x https://tinyurl.com/56zny85x
- tialaramex 5y ago> A trivial example is simply a struct type and its "impl" You're not obliged to implement a type straight after defining the type itself, so without repeating that identifier Rust has no way to be sure which type it is you want to implement. You particularly might want to define several types up front, and then write impl blocks for them later. > have a look at what it takes to refactor even trivial pieces of code in Rust For many similar refactoring jobs Default is adequate. Here Default's derive macro obviously won't guess that you think "blah!" ought to be the default string for some reason (or that miraculously a few lines later now "test" is the default). However it seems like the correct choice here is to implement Default for your new struct ? And you do eventually get to that. But on the way there you go off on a tangent to provide implementations for any arbitrary AsRef<str>, a feature the "equivalent" C# code didn't offer but which apparently was a compelling need for this "refactoring". The types change radically too. If you don't go off on that tangent, you can use Self as you expected, and your solution is much shorter and more robust but does the same thing as the C#. Or, you could modify the C# so that it is fully generic like your Rust solution, after all the Rust solution is able to take any string-like parameter type and the C# only works with the language's built in strings. While you're in there remember that C# "Strings" aren't like Rust's strings, they're just Vec<u16> in Rust terms. There's no promise that these are actually Unicode text. They probably are text but they're only promised to be some even number of bytes presented as ostensibly UTF-16 code units for your convenience.