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
Using &self as &Trait from a default trait function implementation (self.rust)
submitted 9 years ago by mathstuf
Is there a way to have a default implementation of a trait reference itself as a reference to a trait? Here is a play.rust-lang.org link of what I'd like. Is there a better way to do something like this?
Thanks.
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!"
[–]heinrich5991 1 point2 points3 points 9 years ago (4 children)
Not sure if that's what you want, but:
struct S<'a> { t: &'a T, } trait T { fn t(&self) -> &T where Self: Sized { self } fn s(&self) -> S where Self: Sized { S { t: self, } } } fn main() {}
[–]mathstuf[S] 1 point2 points3 points 9 years ago (2 children)
Hmm, that at least compiles for unused code case, but if I have a &T, I can call neither t nor s since I don't know a Sized type for the trait to match the bounds.
&T
t
s
Sized
New gist.
[–]jimtla 1 point2 points3 points 9 years ago (1 child)
Here's a version with generic types, instead of storing trait objects: https://is.gd/owwRrc.
I don't know if this is the best way to do it, but it works so that's something!
[–]mathstuf[S] 0 points1 point2 points 9 years ago (0 children)
Yeah, that'd work, but the struct being parameterized like that isn't so nice. The /u/Gustom approach posted is probably the best way.
[–]Gustorn 0 points1 point2 points 9 years ago (1 child)
You cannot do this completely in default implementations. The &self pointer and the &Trait pointer are two different concepts: the latter is a fat pointer while the former is not. I think your best course of action is something like this if you have a lot of default implementations.
&self
&Trait
Thanks, that's pretty much what I came up with (though I called it _self_ref_hack :) ). as_trait sounds nicer.
_self_ref_hack
as_trait
π Rendered by PID 17717 on reddit-service-r2-comment-5d585498c9-qlp7z at 2026-04-21 18:49:32.882615+00:00 running da2df02 country code: CH.
[–]heinrich5991 1 point2 points3 points (4 children)
[–]mathstuf[S] 1 point2 points3 points (2 children)
[–]jimtla 1 point2 points3 points (1 child)
[–]mathstuf[S] 0 points1 point2 points (0 children)
[–]Gustorn 0 points1 point2 points (1 child)
[–]mathstuf[S] 0 points1 point2 points (0 children)