TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Patterns with Rust Types

116 点作者 mre超过 1 年前

5 条评论

heinrich5991超过 1 年前
As was already mentioned in the Reddit thread on &#x2F;r&#x2F;rust (<a href="https:&#x2F;&#x2F;old.reddit.com&#x2F;r&#x2F;rust&#x2F;comments&#x2F;wb10zi&#x2F;patterns_with_rust_types&#x2F;ii4iki2&#x2F;" rel="nofollow noreferrer">https:&#x2F;&#x2F;old.reddit.com&#x2F;r&#x2F;rust&#x2F;comments&#x2F;wb10zi&#x2F;patterns_with_...</a>): It&#x27;s weird to take a platform-dependent integer for a database ID. Use `u32` or `u64` instead of `usize`.
评论 #37355627 未加载
hardwaregeek超过 1 年前
Builder patterns are also super helpful for libraries. Allows you to create APIs with lots of optional fields without having call sites that pass in a dozen None values.<p>There&#x27;s also the type state pattern where you have a struct take a generic so you can allow certain methods depending on the generic type. Like say you have a struct with a field that you want to be optional, but you know statically that at a certain point in the code it&#x27;ll be set.<p>You can do:<p><pre><code> struct User&lt;IdType&gt; { id: IdType } impl User&lt;()&gt; { fn create() -&gt; User&lt;u64&gt; { ... } } impl User&lt;u64&gt; { fn id() -&gt; u64 { user.id } } </code></pre> That way you can have some methods where you can be certain that the ID is set and some methods where it&#x27;s not set.
pornel超过 1 年前
I love that structs can be strictly non-copyable.<p>This way I don&#x27;t need to heap allocate objects to ensure their uniqueness. I can still modify them in place without worrying there will be any out-of-date copies that weren&#x27;t updated. Or I can give out simple ints or even zero-sized structs that can be handles&#x2F;tokens that grant exclusive or single-use access.
评论 #37355181 未加载
cowthulhu超过 1 年前
Does anyone know of any sites that list out articles like this by language? I always find it tough to locate more advanced or &quot;meta&quot; articles like this that aren&#x27;t just explaining basic concepts.
nmilo超过 1 年前
The no-foreign-traits-on-foreign-types rule and the terrible ergonomics behind the newtype &quot;pattern&quot; combine to make one of the worst development experiences I&#x27;ve ever seen in a language. It basically means you can only use one large library, ever. (With caveats.) For example, if I&#x27;m writing a game, I include a math library and pull in a Vec3 type, then add Vec3 as a member to all my objects, and then later I want to pull in a serialization library that defines, for ex, a Serializable trait, I can&#x27;t, unless:<p>1. I write my own Vec3 class, not fun.<p>2. I write my own serialization library, could be fun depending on who you are but surely a waste of time.<p>3. I make a newtype, implement Serializable on it, find-and-replace all Vec3s with my own SerializableVec3, fix about 500 errors one by one by adding .0 everywhere, implement wrappers for all the Vec3 methods I want to use, rinse and repeat for every other foreign type I want to serialize.<p>4. Hope the math library and the serialization library know each other and the math library implements all the traits you need. In practice this is what happens, but this makes a weird situation where serde is the de facto standard&#x2F;monopoly because it is literally impossible to use anything else. Which may be fine for now, serde is the standard precisely because it&#x27;s so extensible, but if you ever want to use another library with traits, well you&#x27;re SOL and it&#x27;s back to step 3.<p>How they managed to ship a language with dev ergonomics this bad is beyond me. Especially when the solution is so simple. Allow foreign traits on foreign types for executable projects only. Put it behind a compiler flag for all I care. -fallow-foreign-traits-I-dont-care-if-this-breaks-things. Just let me implement my damn traits, the current state is ridiculous.
评论 #37354202 未加载
评论 #37355317 未加载
评论 #37355586 未加载
评论 #37354105 未加载
评论 #37354065 未加载
评论 #37354384 未加载
评论 #37354023 未加载
评论 #37354437 未加载
评论 #37355305 未加载
评论 #37364942 未加载
评论 #37354781 未加载
评论 #37353989 未加载
评论 #37355506 未加载