3 ms·
It usually does that on iterator chains, which AFAIK do not exist as such in C++, so multiple operations would be expressed as multiple imperative statements.
by sph 4y ago
It usually does that on iterator chains, which AFAIK do not exist as such in C++, so multiple operations would be expressed as multiple imperative statements.
My C++ is rusty (no pun intended) but I struggle to imagine their variant of `vector.iter().map().collect()` to be as concise and fit in fewer than 4 lines.
I wonder if OP's C++ port doesn't use iterators that much, and how idiomatic it is.
EDIT: the code is not idiomatic at all.
- strager 4y ago> I wonder if OP's C++ port doesn't use iterators that much, and how idiomatic it is. I think I only used iterators in places where there's no built-in function on slices like C++'s strchr and strspn. (I think Rust's str has these, but not [u8].) For example: C++: https://github.com/quick-lint/cpp-vs-rust/blob/f8d31341f5cac5d0e676ead553a3610284d47a3e/cpp/src/quick-lint-js/i18n/locale.cpp#L38-L43 https://github.com/quick-lint/cpp-vs-rust/blob/f8d31341f5cac... std::size_t length = std::strcspn(c, separators); if (c[length] == '\0') { return found_separator{.length = length, .which_separator = static_cast<std::size_t>(-1)}; } const char* separator = std::strchr(separators, c[length]); Rust: https://github.com/quick-lint/cpp-vs-rust/blob/f8d31341f5cac5d0e676ead553a3610284d47a3e/rust/libs/i18n/src/locale.rs#L53-L64 https://github.com/quick-lint/cpp-vs-rust/blob/f8d31341f5cac... match s .as_bytes() .iter() .position(|c: &u8| separators.contains(c)) { None => FoundSeparator { length: s.len(), which_separator: INVALID_WHICH_SEPARATOR, }, Some(length) => { let found_separator: u8 = unsafe { *s.as_bytes().get_unchecked(length) }; match separators.iter().position(|c: &u8| *c == found_separator) {
- mgaunard 4y agoOf course it exists in C++, and has done since before Rust even existed. Syntax is usually `vector | map | collect`.
- sph 4y agoWow, my C++ knowledge is even worse than I thought. I didn't know it had "pipelines". https://en.cppreference.com/w/cpp/ranges https://en.cppreference.com/w/cpp/ranges
- maleldil 4y agoIt's not "pipelines". It's just an overloaded bitwise-or operator.
- strager 4y ago> Of course it exists in C++, and has done since before Rust even existed. Not in C++'s standard library until C++20.
- mgaunard 4y agoThings don't need to be standardized in an ISO document to exist and be readily available. I remember using it as early as 2008.
- gpderetta 4y ago> It usually does that on iterator chains, which AFAIK do not exist as such in C++, so multiple operations would be expressed as multiple imperative statements. https://en.cppreference.com/w/cpp/ranges https://en.cppreference.com/w/cpp/ranges Before C++20, similar functionality has been available in boost.