diff options
| author | Kyren223 <contact@kyren.codes> | 2026-10-06 20:46:31 +0300 |
|---|---|---|
| committer | Kyren223 <contact@kyren.codes> | 2026-10-06 20:46:31 +0300 |
| commit | ba1be9573ef20d6424ae987257a1c727c47d4b53 (patch) | |
| tree | e59ef56f19142215070d0516a9b3a110d15557b6 /src/content/blogs/rust-is-great.md | |
| parent | ea056b6e080669ba4b6f16c2757b4e2e88ea0553 (diff) | |
Initial setup for website rewrite
Diffstat (limited to 'src/content/blogs/rust-is-great.md')
| -rw-r--r-- | src/content/blogs/rust-is-great.md | 101 |
1 files changed, 0 insertions, 101 deletions
diff --git a/src/content/blogs/rust-is-great.md b/src/content/blogs/rust-is-great.md deleted file mode 100644 index 6916867..0000000 --- a/src/content/blogs/rust-is-great.md +++ /dev/null @@ -1,101 +0,0 @@ ---- -title: "Rust is great... except for the language" -description: "" -date: 2026-08-27 ---- - -For those who know me it might be weird seeing me say "Rust is great", after all, I have been -openly bashing and criticize the language for quite a while now. - -While my opinion on Rust hasn't changed, I came to a realization that made me appreciate Rust -for what it has taught me -- which I hope to share in this blog. - -## What is Rust? - -> Rust is a blazingly fast and memory-efficient language: with no runtime or garbage collector, -> Rust's rich type system and ownership model guarantees memory-safety and thread-safety, -> enabling you to eliminate many classes of bugs at compile-time. - -Well... at least that's how the Rust website describes the language, in reality this is mostly -propaganda, it's inaccurate and misleading at best. - -A more fair definition might look like: - -Rust is a language that restricts the way you are allowed to program, enabling it to make certain -guarantees that allows detecting memory and data corruption at compile time. -Rust doesn't have a garbage collector, making it usually faster than garbage-collected languages. - -So, both of these definitions seem to focus on Rust being able to guarantees that usually only -garbage collected languages can make, so how does it do that? - -Rust accomplishes that via it's most unique feature: the Ownership Model. - -## What is Ownership? - -There are 3 rules to ownership: - -- Each value in Rust has an owner -- There can only be one owner at a time -- When the owner goes out of scope, the value will be dropped (freed/deallocated) - -Assigning a value to a variable, passing it as an argument to a function[^1] or returning it will -result in what's called a "move", where the existing variable gives up it's ownership and passes -it to a new owner. - -This mostly applies to types that allocate a resource that needs freeing, which in Rust means any -type that implements the `Drop` trait. -Types that don't implement `Drop` such as integers and other primitives are "trivially copyable" -meaning the value just gets copied instead of "moving" ownership. - -Just these rules already make a few nice guarantees: - -- Values are only dropped exactly once, preventing double frees -- Values can't be used after being dropped, preventing use-after-free (UAF) -- Values will be automatically dropped, preventing SOME memory leaks. - -One issue with this, is that if you pass a few values to a function temporarily, you must return -all of them back, otherwise the caller can no longer use them. - -This is fixed by another concept called "borrowing" -- as the the name suggests, instead of giving -up ownership, it allows to "borrow" a _reference_ to a value temporarily, and later return the -reference back to owner. - -There are 2 types of references, a read-only (immutable) reference, and a read-write (mutable) -reference. By restricting the programmer to only ever have a single mutable reference OR any -number of immutable references Rust is able to guarantee a bit more: - -- Data races can't happen as a value will never be written to while also being read/written by - something else -- References are always valid (no dangling references) - -There is one more concept worth mentioning, but first, I'd like to take this opportunity to shine -a light on one of my biggest issues with the language -- the restrictiveness of it. - -While these rules do give us guarantees, they make our lives incredibly tedious, we constantly -have to keep track of what's mutable, and what isn't, and what references do we have to what, -and if we get any of that wrong, we have to stop what we are doing, read a long error message, fix -the bug, and then go back and try to remember what we actually wanted to do - and then again, -every few minutes, it destroys productivity. - -And it doesn't even need to be a complex example, even this simple code doesn't compile: -```rust -let my_list = vec![1, 2, 3, 4]; -let first: &i32 = my_list[0]; -my_list.push(5); // error: can't take mutable reference because immutable reference exists (first) -let last: &i32 = my_list[my_list.len-1]; -println!("{first} and {second}"); -``` - -This is a very simple code example that doesn't violate any of the guarantees[^2] Rust makes, yet -Rust does not compile this code. This is because not every valid program would be verifiable by -Rust, there will always be correct programs that Rust will deny. - -## Lifetimes - - - - - - - - |
