7 ms·
Rust uses `repeat()`, which sounds much more descriptive to me. The types in the function signature make the "clone" part of the name redundant.
by proto_lambda 4y ago
Rust uses `repeat()`, which sounds much more descriptive to me. The types in the function signature make the "clone" part of the name redundant.
- mjevans 4y agoOffhand, is repeat(0) an empty string, repeat(1) the input string, etc? If so that's a great name for the function.
- pezezin 4y agoRepeat is an iterator, so you can apply it to any type you want, not just strings. You can chain it with other iterators, or collect it into some data structure. But yes, repeat(0) returns an empty iterator. https://doc.rust-lang.org/std/iter/fn.repeat.html https://doc.rust-lang.org/std/iter/fn.repeat.html
- tialaramex 4y agoThis is wrong, repeat(0) gives you a Repeat which will repeat 0 forever, that's its purpose: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=17c35b3b0e9a84b0195600ebf2080729 https://play.rust-lang.org/?version=stable&mode=debug&editio... This example makes a repeat(0) but asks for just the first 12 things in it, they are, of course, all twelve zeroes. Feel free to adjust it to ask for more if you think maybe there aren't any more, or they stop being zero.
- pezezin 4y agoShit, I should not post in the morning before my first coffee :(
- proto_lambda 4y agoThat's the wrong repeat(), I was talking about str::repeat(): https://doc.rust-lang.org/std/primitive.str.html#method.repeat https://doc.rust-lang.org/std/primitive.str.html#method.repe...
- pezezin 4y agoShit, I should not post in the morning before my first coffee :(
- deleted 4y ago[deleted]
- tialaramex 4y agoI originally wrote a reply here that I deleted because I realised somebody introduced the wrong idea below. Yes, the repeat() function on strings does what you describe. https://doc.rust-lang.org/std/primitive.str.html#method.repeat https://doc.rust-lang.org/std/primitive.str.html#method.repe... str::repeat() behaves exactly as you describe. "No".repeat(4) is "NoNoNoNo" and "What".repeat(0) is "" and so on. Note that str::repeat() returns a String, not a str, because if n > 1 it obviously needs to allocate somewhere to put the String. As a result it is not available in environments which don't have String, like a tiny embedded device - for them str::repeat() does not exist whereas stuff like str::starts_with() and str::split_once() are fine.