Yesterday, I eked out a bit of time to contribute to Artichoke in Rust, in no small part due to the maintainer @lopopolo:
Artichoke is an implementation of Ruby written in the Rust programming language. The issue I worked on was to expose a newly stabilized method in Artichoke’s implementation of the Ruby String class. In Rust, strings are represented as Vectors of u8
bytes which are guaranteed to be valid UTF-8 sequences. As of Rust version 1.56.0, the compiler stabilized the Vec::shrink_to method, which allows you to reduce the amount of memory a Vec takes, down to a lower bound, which you supply. So if you have a String and it has allocated 4 bytes, but it’s only taking up 1 byte, you can string.shrink_to(2)
to reduce its size to no less than 2 bytes. Here’s that same scenario in code:
let mut string = String::with_capacity(4);
string.extend("1".chars());
assert_eq!(string.capacity(), 4);
string.shrink_to(2);
assert!(string.capacity() >= 2);
string.shrink_to(0);
assert!(string.capacity() >= 1);
The code I worked on exposed the same methods on Artichoke’s String classes: Implements `shrink_to` on `spinoso_string::String` by briankung ยท Pull Request #1505 ยท artichoke/artichoke. The actual code, excluding the doctests, was very minimal:
#[inline]
pub fn shrink_to(&mut self, min_capacity: usize) {
self.buf.shrink_to(min_capacity);
}
But what I liked about this was how easy @lopopolo made it to contribute! From giving me the initial nudge and motivation to build Artichoke locally for the first time to the extensive guidance in the issue, it was a great way to get some open source experience on a non-trivial Rust project. I can’t recommend it enough. In fact, I can point you to a few issues that could use a similar treatment:
This was a great experience for me, and @lopopolo’s stewardship of the project reminded me of both Ruby’s value of niceness and Rust’s value of inclusivity. It led directly to a second PR and I hope to keep contributing in the future to learn more about both Rust and Ruby.
Links:
- The Artichoke Ruby project: https://github.com/artichoke/artichoke
- The Artichoke public Discord server: https://discord.gg/QCe2tp2
Leave a Reply