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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Comparing Rust and JavaScript Ergonomics with a Simple Linked List

59 点作者 codesections超过 6 年前

9 条评论

jerf超过 6 年前
Heh, I feel like &quot;someone&quot; ought to write the Big List of Bad First Projects for This Language, e.g.:<p>Go&#x2F;Erlang&#x2F;Elixir: &quot;Testing&quot; the concurrency by parallelizing the addition of an array of integers via some sort of sending single integers over messages, or in the worst cases, literally spawning entire processes&#x2F;goroutines&#x2F;etc. to add two ints (and then wondering why the concurrent program fully consumes all eight CPUs but is still twenty+ times slower).<p>Go (as of this writing): Immediately trying to implement a generic data structure.<p>Rust: Complicated pointer-type data structures like linked lists, or goodness-forbid, doubly-linked lists.<p>Haskell: Starting right off by trying to implement an in-place sorting algorithm.<p>Python: Taking your C numeric algorithm and converting it into pure Python (and then being shocked at the performance).<p>Criteria for my inclusion is that I personally have seen each of these many times.<p>It&#x27;s not that these tasks are necessarily impossible, or even necessarily all that hard when you know what you are doing, just that they are <i>bad first tasks</i>, but they seem to tempt people for some reason. For example, someone reading a Python tutorial hasn&#x27;t heard of numpy, if you&#x27;re just learning Rust immediately learning how to break the rules you don&#x27;t fully understand yet isn&#x27;t the best use of your time, etc.<p>In this particular case it all turned out OK in the end, but it often doesn&#x27;t go this well. :)
评论 #18847703 未加载
评论 #18847711 未加载
评论 #18848081 未加载
评论 #18848678 未加载
pornel超过 6 年前
Oh no, anything but the linked list.<p>Real-world Rust programs don&#x27;t implement linked lists. There&#x27;s LinkedList in libstd if you really wanted one, but in modern architectures cache locality is so important that almost always some other data structure is a better choice. Rust&#x27;s standard library and crates.io have plenty of containers to choose from.<p>Linked list is popular because it&#x27;s a CS101 topic, and because C is so barren. When you don&#x27;t have any containers in stdlib, dependency management is hell, and due to lack of generics any 3rd party implementation will be either inefficient or full of macros, only then low-effort hand-rolled linked list seems like a sensible choice.
评论 #18848499 未加载
评论 #18847989 未加载
评论 #18848702 未加载
Shebanator超过 6 年前
I&#x27;m baffled how the author could look at these code samples and say that the Rust and Javascript versions were mostly identical.
cryptonector超过 6 年前
&gt; What&#x27;s more, according to that book, there simply isn&#x27;t a good way to implement this structure in safe Rust. The way to go is to venture into unsafe Rust.<p>Noooo, you don&#x27;t want to do that. Just give up on circular references, doubly-linked lists, and so on. Use different data structures and work around the [perceived] downsides of not having the data structures you&#x27;re used to in Lisp, C, ECMAScript, ... You&#x27;ll find it pays off.<p>See this HN post for more: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=18098239" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=18098239</a>
desireco42超过 6 年前
If I had a dollar each time I needed to use and implement single linked list... (I wouldn&#x27;t have many dollars)
评论 #18848431 未加载
offbytwo超过 6 年前
&gt;Rust is pretty<p>&gt;Of course, you might feel differently, but one of my biggest takeaways from all of this side-by-side code is that Rust is clear, expressive<p>As someone who doesn&#x27;t use either of these languages, I couldn&#x27;t disagree more. The Javascript was intuitive and immediately made sense, while the Rust code gave me a headache.
codezero超过 6 年前
I&#x27;d like to see this comparison, but with TypeScript instead of JavaScript. I&#x27;m sure it will still be better than Rust, but it seems like the main thing making Rust really awkward here is the memory safety.
simplify超过 6 年前
Can someone explain why raw_tail was necessary in the add_to_tail function?
评论 #18848371 未加载
Scarbutt超过 6 年前
Found the JS implementation horrible, he could just have use plain objects (would be fair since he used a plain struct in rust) and functions instead of &#x27;this&#x27; everywhere.
评论 #18848461 未加载