首页

Rust's Ugly Syntax (2023)

161 点作者 nequo9 个月前

19 条评论

sedatk9 个月前
I think the article makes a good point, but the actual example isn’t Rust’s worst, not even close. It gets really hard to follow code when multiple generic types are combined with lifetime markers. Then it truly becomes a mess.
评论 #41398877 未加载
评论 #41398409 未加载
评论 #41398735 未加载
评论 #41398842 未加载
评论 #41403639 未加载
评论 #41398254 未加载
wiz21c9 个月前
For my own situation, the articles present the right way to express all possible performance&#x2F;error handling (which is expected in a standard lib) and then goes on to show how I actually code it in my own softawre where I don&#x27;t really need the level of detail&#x2F;finetuning of the standard lib.<p>Interestingly, my life starts at the end of the article, with the simple verison of the code, and as my understanding of rust widens, I go up to the beginning of the article and better define my function...
评论 #41398458 未加载
tmtvl9 个月前
Aw, no Rasp variant? Let&#x27;s brainstorm it up...<p><pre><code> (defun read (path) (declare (generic P (AsRef Path)) (type P path) (returns (io:Result (Vector U8)))) (flet ((inner (path) (declare (type (Ref Path) p) (returns (io:Result (Vector U8)))) (try-let ((file (File:open path)) (bytes (vector))) (declare (mutable file bytes)) (try (read-to-end file bytes) (Ok bytes))))) (inner (as-ref path))))</code></pre>
MetricExpansion9 个月前
If I understood all the semantic properties, including the separate compilation requirements, correctly, here’s how I think it would be done in Swift with the proposed nonescapable types features (needed to safely express the AsRef concept here). (Note that this doesn’t quite compile today and the syntax for nonescaping types is still a proposal.)<p><pre><code> @usableFromInline func _read(pathView: PathView) throws(IOError) -&gt; [UInt8] { var file = try File(pathView) var bytes: [UInt8] = [] try file.readToEnd(into: &amp;bytes) return bytes } @inlinable public func read&lt;Path&gt;(path: borrowing Path) throws(IOError) -&gt; [UInt8] where Path: PathViewable, Path: ~Copyable { try _read(pathView: path.view()) } &#x2F;&#x2F; Definitions... public enum IOError: Error {} public protocol PathViewable: ~Copyable { func view() -&gt; PathView } public struct PathView: ~Escapable {} public struct File: ~Copyable { public init(_ pathView: borrowing PathView) throws(IOError) { fatalError(&quot;unimplemented&quot;) } public mutating func readToEnd(into buffer: inout [UInt8]) throws(IOError) { fatalError(&quot;unimplemented&quot;) } }</code></pre>
jiwangcdi9 个月前
&gt; The next noisy element is the &lt;P: AsRef&lt;Path&gt;&gt; constraint. It is needed because Rust loves exposing physical layout of bytes in memory as an interface, specifically for cases where that brings performance. In particular, the meaning of Path is not that it is some abstract representation of a file path, but that it is just literally a bunch of contiguous bytes in memory.<p>I can&#x27;t understand this. Isn&#x27;t this for polymorphism like what we do this:<p>```rust fn some_function(a: impl ToString) -&gt; String { a.to_string(); } ```<p>What to do with memory layout? Thanks for any explanation.
评论 #41398712 未加载
eterps9 个月前
Just give me Rattlesnake or CrabML and I&#x27;ll stop complaining :-)
评论 #41398736 未加载
评论 #41398733 未加载
评论 #41398785 未加载
评论 #41398443 未加载
anonymous20249 个月前
I wonder. How does Rust syntax compares with <a href="https:&#x2F;&#x2F;www.hylo-lang.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.hylo-lang.org&#x2F;</a> syntax? That also is memory safe, typesafe, and data-race-free.
librasteve9 个月前
Here&#x27;s the cleaned up version of Rust from the OP:<p><pre><code> pub fn read(path: Path) -&gt; Bytes { let file = File::open(path); let bytes = Bytes::new(); file.read_to_end(bytes); bytes } </code></pre> Here is is in raku (<a href="https:&#x2F;&#x2F;raku.org" rel="nofollow">https:&#x2F;&#x2F;raku.org</a>):<p><pre><code> sub read(Str:D $path --&gt; Buf:D) { $path.IO.slurp: :bin } </code></pre> [the `--&gt; Buf:D` is the raku alternative to monads]
评论 #41403491 未加载
qalmakka9 个月前
People that complain about Rust&#x27;s syntax never have never seen C++ at its worst
评论 #41410622 未加载
tevelee9 个月前
The article just turned Rust into Swift. Nicer syntax, same semantics
apatheticonion9 个月前
Someone needs to tell them about async Rust. Big yikes.
评论 #41401131 未加载
mgaunard9 个月前
There are several problems with the C++ variant, which could have been easily avoided by just following the original Rust more closely.
AxelLuktarGott9 个月前
Is it really better to remove the error case information from the type signature? Aren&#x27;t we losing vital information here?
评论 #41398257 未加载
Woshiwuja9 个月前
So you just end up with python at the end?
macmac9 个月前
My hot take is that Rust should have been a Lisp. Then it could also have had readable macros.
评论 #41399361 未加载
mjburgess9 个月前
Kinda disingenuous, you don&#x27;t reskin one language in another to make an argument about syntax -- you develop a clear syntax for a given semantics. That&#x27;s what rust <i>did not</i> do -- it copied c++&#x2F;java-ish, and that style did not support the weight.<p>When type signatures are so complex it makes vastly more sense to separate them out,<p>Consider,<p><pre><code> read :: AsRef(Path) -&gt; IO.Result(Vec(U8)) pub fn read(path): inner :: &amp;Path -&gt; IO.Result(Vec(U8)) fn inner(path): bytes := Vec.new() return? file := File.open(path) return? file.read_to_end(&amp;! bytes) return OK(bytes) inner(path.as_ref())</code></pre>
评论 #41399144 未加载
评论 #41399264 未加载
评论 #41398364 未加载
评论 #41398936 未加载
评论 #41398744 未加载
评论 #41398648 未加载
评论 #41398471 未加载
remcob9 个月前
Why stop there and not go all the way to<p><pre><code> pub fn read(path: Path) -&gt; Bytes { File::open(path).read_to_end() }</code></pre>
评论 #41398362 未加载
singularity20019 个月前
<p><pre><code> &quot;I think that most of the time when people think they have an issue with Rust’s syntax, they actually object to Rust’s semantics.&quot; </code></pre> You think wrong. Rust syntax is horrible because it is verbose and full of sigils
评论 #41401027 未加载
oguz-ismail9 个月前
The final version is still ugly. Why `pub fn&#x27;? Why is public not the default and why do you have to specify that it&#x27;s a function? Why `: type&#x27; and `-&gt; type&#x27;, why can&#x27;t type go before the identifier? Why do you need `File::&#x27; and `Bytes::&#x27;? What is that question mark? Why does the last statement not need a semicolon? It&#x27;s like the opposite of everything people are used to.
评论 #41399176 未加载
评论 #41398347 未加载
评论 #41398289 未加载
评论 #41398420 未加载