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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Ask HN: Rust topics you wish someone had explained when first getting started?

13 点作者 Toidiu将近 8 年前
So I am preparing to give a getting-started talk on Rust.<p>I was wondering what are some topics that people would have liked an explanation for when they first started learning Rust?

3 条评论

_jordan将近 8 年前
I wish someone would have explained how to approach learning Rust; It&#x27;s a really hard language to grasp - when struggling, I think it&#x27;s important to really understand <i>why</i> whatever you&#x27;re doing is hard - to learn where your misunderstanding and errors are fundamentally. This is non technical but might be worth mentioning.
评论 #15026922 未加载
评论 #15032485 未加载
评论 #15027897 未加载
dumindunuwan将近 8 年前
This is my approach,<a href="https:&#x2F;&#x2F;medium.com&#x2F;learning-rust" rel="nofollow">https:&#x2F;&#x2F;medium.com&#x2F;learning-rust</a><p>The ORDER of we are learning Rust<p>It&#x27;s less useful to explain about language capabilities by examples or explain about lifetimes before structs, enums<p>▸ Installation &amp; Hello World ▸ Cargo &amp; Crates ▸ Variable bindings , Constants &amp; Statics ▸ Comments ▸ Functions ▸ Primitive Data Types ▸ Operators ▸ Control Flows<p>▸ Vectors ▸ Structs ▸ Enums ▸ Generics ▸ Impls &amp; Traits<p>▸ Ownership▸ Borrowing▸ Lifetimes &amp; Lifetime Elision<p>▸ Modules ▸ Crates ▸ Workspaces ▸ Error Handling<p>▸ Functional programming in Rust
评论 #15032500 未加载
neuronsguy将近 8 年前
Some things to avoid early on so you can get a working app out quickly:<p>1. Don&#x27;t build OO abstractions, just use structs as data and operate on it with free functions.<p>2. Don&#x27;t be afraid of long argument lists with lots of references, where the last one is possibly mutable, C-style<p>3. If something needs e.g. 2 different structs as inputs and mutates a third one, make it a free function, don&#x27;t impl it on any of the structs.<p>4. Don&#x27;t overdo it trying to use the functional idioms for options and results like and_then or_else etc. Just use match and eventually it will be obvious where those fit.<p>5. Don&#x27;t worry too much about error handling, just use expect and explicit panics<p>6. Don&#x27;t implement your own data structures, compose the std lib ones<p>7. Get good at using match and enums to represent state and transitions<p>Once you&#x27;ve got some more experience and you want to try to write more elegant code, you will run into ownership issues.<p>1. Use guard types. Basically a struct that owns a pointer into some larger data. Write a lot of these with useful utilities for manipulating that data<p>2. Use guard types for mutating data as well. I tend to have a type which is a big bag of data (often carefully laid out in memory for performance), and then separate types whose only purpose is to contain references to e.g. 2 or 3 different pieces of data that need to be simultaneously used. That type allows you to do whatever that operation is. This saves a lot of borrow checker fighting.<p>3. Think in a data centric way. What data do you need? What operations need to be performed? Then design your ownership hierarchy such that that can happen.<p>Finally you will run into frustration structuring large projects.<p>1. Use cargo workspaces. Isolate self contained chunks of functionality into crates.<p>2. Implement stdlib traits like Iterator, From, Into<p>3. You can use associated types to do statically checked dependency injection. In combination with default trait implementations it&#x27;s a decent way.<p>4. Use free functions liberally inside modules, but export mostly traits, data structs, and operation structs.<p>5. Use rustdoc comments in your code and refer to them.<p>A lot of the above is opinion and might not work for everyone. I submit that it&#x27;s &quot;a path&quot; of many for getting productive quickly and then scaling up to writing larger systems.<p>(Crosspost of a comment I made on Reddit: <a href="https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;rust&#x2F;comments&#x2F;6rxw21&#x2F;comment&#x2F;dlb048x" rel="nofollow">https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;rust&#x2F;comments&#x2F;6rxw21&#x2F;comment&#x2F;dlb048...</a>)
评论 #15032545 未加载