When you want your return types to be immutable

So there was this question on Reddit: https://www.reddit.com/r/rust/comments/96w29j/hey_rustaceans_got_an_easy_question_ask_here/e4d0umh/

I came up with an interesting solution:

I thought it worth hoisting up to its own post (here). Perhaps I'm mistaken and such a thing already exists, but I couldn't think of any built-in way or existing crate that would allow a function to return a value, but have that value be immutable to the caller.

But why? Honest question; name a use case.

Rust's borrow checker already solves most of the problems that immutable values might be needed for in other languages.

1 Like

I'm with @ExpHP on this one.

If the method gave away ownership of its return value, it cannot possibly care whether that value is modified later, since it cannot look at it. So why bother prohibiting it? The only immutable wrapper you need is & :slightly_smiling_face:

1 Like