Rust's Ugly Syntax (2023)
The blog post addresses complaints about Rust's syntax, attributing them to misunderstandings of its semantics. It suggests simplifying semantics for readability while maintaining performance and safety features.
Read original articleThe blog post discusses the common complaints regarding Rust's syntax, suggesting that many of these complaints stem from misunderstandings of Rust's semantics rather than the syntax itself. The author presents a function that reads the contents of a binary file, illustrating its syntax and how it could be perceived as "ugly." Various alternative syntaxes from hypothetical programming languages are provided to highlight how different syntax could look. The author then explores simplifying Rust's semantics while retaining its syntax, ultimately proposing a version of the function that is less complex and more readable. This involves removing nested functions, generic constraints, and excessive error handling, which could lead to a more straightforward implementation. The post concludes that while Rust's syntax may seem cumbersome, it serves specific performance and safety purposes that are integral to the language's design.
- Many complaints about Rust's syntax are actually related to its semantics.
- The author provides examples of how Rust's syntax could be simplified while maintaining functionality.
- Simplifying Rust's semantics can lead to more readable code, but may sacrifice some performance benefits.
- The discussion emphasizes the balance between syntax complexity and the language's safety and performance features.
Related
I Hope Rust Does Not Oxidize Everything
The author expresses concerns about Rust's widespread adoption in programming, citing issues with syntax, async features, complexity, and long compile times. They advocate for language diversity to prevent monoculture, contrasting Rust with their language Yao.
Rustgo: Calling Rust from Go with near-zero overhead
An experiment explores calling Rust code from Go to enhance performance in critical applications. It proposes linking Rust directly into Go, avoiding cgo overhead, and details calling conventions for efficient integration.
Don't write Rust like it's Java
The author discusses transitioning from Java to Rust, highlighting Rust's type safety, differences in traits and interfaces, complexities of ownership, and the importance of embracing Rust's unique features for effective programming.
Language Compilation Speed (2021)
The article examines Rust's compilation speed compared to C/C++, noting frustrations among developers. It proposes a benchmarking method, revealing GCC compiles at 5,000 lines per second and Clang at 4,600.
From Julia to Rust
The article outlines the author's transition from Julia to Rust, highlighting Rust's memory safety features, design philosophies, and providing resources for learning, while comparing code examples to illustrate syntax differences.
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...
(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))))
@usableFromInline
func _read(pathView: PathView) throws(IOError) -> [UInt8] {
var file = try File(pathView)
var bytes: [UInt8] = []
try file.readToEnd(into: &bytes)
return bytes
}
@inlinable
public func read<Path>(path: borrowing Path) throws(IOError) -> [UInt8] where Path: PathViewable, Path: ~Copyable {
try _read(pathView: path.view())
}
// Definitions...
public enum IOError: Error {}
public protocol PathViewable: ~Copyable {
func view() -> PathView
}
public struct PathView: ~Escapable {}
public struct File: ~Copyable {
public init(_ pathView: borrowing PathView) throws(IOError) {
fatalError("unimplemented")
}
public mutating func readToEnd(into buffer: inout [UInt8]) throws(IOError) {
fatalError("unimplemented")
}
}
I can't understand this. Isn't this for polymorphism like what we do this:
```rust fn some_function(a: impl ToString) -> String { a.to_string(); } ```
What to do with memory layout? Thanks for any explanation.
pub fn read(path: Path) -> Bytes {
let file = File::open(path);
let bytes = Bytes::new();
file.read_to_end(bytes);
bytes
}
Here is is in raku (https://raku.org): sub read(Str:D $path --> Buf:D) {
$path.IO.slurp: :bin
}
[the `--> Buf:D` is the raku alternative to monads]When type signatures are so complex it makes vastly more sense to separate them out,
Consider,
read :: AsRef(Path) -> IO.Result(Vec(U8))
pub fn read(path):
inner :: &Path -> IO.Result(Vec(U8))
fn inner(path):
bytes := Vec.new()
return? file := File.open(path)
return? file.read_to_end(&! bytes)
return OK(bytes)
inner(path.as_ref())
pub fn read(path: Path) -> Bytes {
File::open(path).read_to_end()
}
"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."
You think wrong. Rust syntax is horrible because it is verbose and full of sigilsRelated
I Hope Rust Does Not Oxidize Everything
The author expresses concerns about Rust's widespread adoption in programming, citing issues with syntax, async features, complexity, and long compile times. They advocate for language diversity to prevent monoculture, contrasting Rust with their language Yao.
Rustgo: Calling Rust from Go with near-zero overhead
An experiment explores calling Rust code from Go to enhance performance in critical applications. It proposes linking Rust directly into Go, avoiding cgo overhead, and details calling conventions for efficient integration.
Don't write Rust like it's Java
The author discusses transitioning from Java to Rust, highlighting Rust's type safety, differences in traits and interfaces, complexities of ownership, and the importance of embracing Rust's unique features for effective programming.
Language Compilation Speed (2021)
The article examines Rust's compilation speed compared to C/C++, noting frustrations among developers. It proposes a benchmarking method, revealing GCC compiles at 5,000 lines per second and Clang at 4,600.
From Julia to Rust
The article outlines the author's transition from Julia to Rust, highlighting Rust's memory safety features, design philosophies, and providing resources for learning, while comparing code examples to illustrate syntax differences.