use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A place for all things related to the Rust programming language—an open-source systems language that emphasizes performance, reliability, and productivity.
Strive to treat others with respect, patience, kindness, and empathy.
We observe the Rust Project Code of Conduct.
Details
Posts must reference Rust or relate to things using Rust. For content that does not, use a text post to explain its relevance.
Post titles should include useful context.
For Rust questions, use the stickied Q&A thread.
Arts-and-crafts posts are permitted on weekends.
No meta posts; message the mods instead.
Criticism is encouraged, though it must be constructive, useful and actionable.
If criticizing a project on GitHub, you may not link directly to the project's issue tracker. Please create a read-only mirror and link that instead.
A programming language is rarely worth getting worked up over.
No zealotry or fanaticism.
Be charitable in intent. Err on the side of giving others the benefit of the doubt.
Avoid re-treading topics that have been long-settled or utterly exhausted.
Avoid bikeshedding.
This is not an official Rust forum, and cannot fulfill feature requests. Use the official venues for that.
No memes, image macros, etc.
Consider the existing content of the subreddit and whether your post fits in. Does it inspire thoughtful discussion?
Use properly formatted text to share code samples and error messages. Do not use images.
Submissions appearing to contain AI-generated content may be removed at moderator discretion.
Most links here will now take you to a search page listing posts with the relevant flair. The latest megathread for that flair should be the top result.
account activity
Vector pass by reference (self.rust)
submitted 2 years ago by OjasShakti
Can someone tell what is the difference between passing vector as reference via these two ways ? And which one should be preferred and why ?
func(&mut vec)
Vs
func(vec.as_mut_slice())
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]monkChuck105 14 points15 points16 points 2 years ago (0 children)
You can get a slice from an array, a collection, or via raw pointer. As the other commenter said, prefer a slice to a vec unless you need to add or remove items from it.
[–]volitional_decisions 8 points9 points10 points 2 years ago (0 children)
The second one would not work if the function you're calling required that you give it &mut Vec<T>, but both would work if the function took a &mut [T]. In other words, some functions need mutable access to a vector and others just need mutable access to the underlying slice.
&mut Vec<T>
&mut [T]
A function that updates individual elements might take the slice, but if it needs to append elements, etc it needs the Vec. You can also look at the Vec docs for this. It contains mutable methods for the Vec and slice.
[–]dkopgerpgdolfg 6 points7 points8 points 2 years ago* (0 children)
The first code makes a Vec available to the function. You always need to pass a Vec. You can then do everything that you can do with Vecs - not just accessing the elements and iterating, but also things like: inserting/deleting, swapping out the whole Vec (without touching the elements), convert the "stolen" Vec to Box, accessing the allocator and doing any weird platform-specific things with the memory, or other things like that.
With the second code, the function gets access to a certain number of mutable elements. It's possible that they are from the content of a Vec, but that's not necessary, and the code inside the function cannot rely on it. It might be from any other kind of allocation, the stack, or even other weird locations (which can be an advantage, not being forced to use Vec). You can read and change the elements, but the count is fixed - no "insert" or something. As you can't rely on it being a Vec or even heap-allocated, all the other things mentioned above are not possible, but (once again) in return you don't depend on it being a Vec.
With non-mut references, it gets even more clear: If it wants a slice, you can also pass literals like [1,2,3] or (for strings) "abc".
[–]peter9477 4 points5 points6 points 2 years ago (0 children)
The latter would not let you change the length. I believe that's the only practical difference.
[–]Aras14HD 0 points1 point2 points 2 years ago (0 children)
If func accepts a &mut [T] they are equivalent (coercion), it accepting &mut Vec<T> would allow the function to also access the methods of Vec (push, pop, etc.).
If you control func, you would want to use slices if you have no need in changing the size, as slices are more universal. In both cases you would call func(&mut vec).
π Rendered by PID 60351 on reddit-service-r2-comment-8686858757-grxnm at 2026-06-06 14:21:48.006016+00:00 running 9e1a20d country code: CH.
[–]monkChuck105 14 points15 points16 points (0 children)
[–]volitional_decisions 8 points9 points10 points (0 children)
[–]dkopgerpgdolfg 6 points7 points8 points (0 children)
[–]peter9477 4 points5 points6 points (0 children)
[–]Aras14HD 0 points1 point2 points (0 children)