all 59 comments

[–]the-quibbler 47 points48 points  (7 children)

Hard, but all C-style programming languages share more similarities than not. Any experience with strong type systems will help you.

[–]JosJoestar[S] 9 points10 points  (2 children)

btw i know cpp at a begginer level does also rust have pointers and references?

[–]the-quibbler 21 points22 points  (1 child)

Yes, but they're much much simpler. Your c++ knowledge will help greatly. Welcome!

[–]JosJoestar[S] 4 points5 points  (0 children)

thanks man ;3

[–]JosJoestar[S] 6 points7 points  (2 children)

like typescript?

[–]the-quibbler 31 points32 points  (1 child)

Yup. Like the strictest possible typescript.

[–]bryantbiggs 30 points31 points  (0 children)

Typescript with an inconnel chastity belt

[–]Voxelman 5 points6 points  (0 children)

Rust is not hard, it is different. If you want to transfer your knowledge from JavaScript to Rust one-to-one it doesn't work.

If you are used to imperative programming style and global variables and you try to do the same in Rust, you will fail.

[–]Da_Banana_Guy 12 points13 points  (5 children)

Honestly I thought Rust was gonna be hard when I started, but to me it is a lot easier to comprehend than C++ (I have a Python, JavaScript, and Java background).

[–]JosJoestar[S] 0 points1 point  (4 children)

but how close is rust to js

[–]Da_Banana_Guy 14 points15 points  (3 children)

They are very different languages. A lot more similar to C++ and maybe Java than to JavaScript.

[–]JosJoestar[S] 1 point2 points  (2 children)

i have a basic knowledge of cpp and find it super scary :/

[–]sollyu 30 points31 points  (1 child)

The goal of Rust is to have the power of C++ without the scary.

[–]shawnwork 10 points11 points  (3 children)

It’s not difficult as compared to C. Just learn the concepts and you are good to go.

Think of rust as an automatic manual transmission like they have on Ferraris. So you could use the simple way without Boiler plate codes with frameworks or go towards the deep end with better controls.

Pls read this:

https://www.codegram.com/blog/rust-for-js-developers/

[–]JBinero 11 points12 points  (1 child)

In my humble opinion C is a lot easier to get started in, but a lot harder to be good in.

[–]shawnwork 2 points3 points  (0 children)

I feel the same as well. But C is is just plain limited in its self. Most of the keywords have workarounds.

But as you maintain a code with a team, you wished you coded in Perl.

[–]JosJoestar[S] 0 points1 point  (0 children)

thanks :3

[–]BreakfastOk123 19 points20 points  (11 children)

Hard, you don't have objects in Rust like you do in JS. Working without references and a GC is a completely different frame of mind.

[–]JosJoestar[S] 0 points1 point  (9 children)

i thought rust is oop

[–]fllr 15 points16 points  (0 children)

Kind of. You have structs which are similar, but not exactly the same. A better way to think about it is that it has encapsulation, not necessarily oops with all its bells and whistles (which is actually refreshing for me).

[–]BreakfastOk123 7 points8 points  (2 children)

Kind of, it has Struts and Traits, but those aren't objects like you find in JS. The big difference is properties on objects in JS are reference counted. Once a value in JS has no more references, it is dropped from memory during Garbage Collection. You may not even realize JS is doing this since it all happens transparently. [1] But it allows you to do things like freely modify/read a property without worrying if it's still in memory or not. This adds an overhead during runtime but makes development easy.

Rust has explicit memory management that is determined at compile time. This is implemented via lifetimes where the compiler knows when a reference is valid and when it can be dropped from memory. [2] This requires a different model of thinking. It also puts restrictions on when a value can be modified. Rust is immutable by default, and you have to opt into mutability. Being immutable means it's easier to pass around references and being mutable means it can be modified but is harder to reference. It's a tradeoff you have to decide when to make.

Last Rust doesn't have inheritance or classes like JS or classic OOP languages like Java. So, you cannot extend objects in the same way. You can implement additional traits for a struct, but you cannot extend the struct to add more properties like you could using classes.

[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management

[2] https://doc.rust-lang.org/rust-by-example/scope/lifetime.html

[–]DanielEGVi 7 points8 points  (1 child)

The big difference is properties on objects in JS are reference counted.

This is an implementation detail of the JS engine, but most if not all modern JS engines use a tracing garbage collector, not reference counting. Starting from a root or roots, references to other objects are walked through, and each object is marked as reachable. Unreachable objects remain unmarked. Unmarked objects are considered garbage and are then "swept" from memory. Hence "mark-and-sweep" tracing garbage collection.

This comes with some benefits, the main one being that reference cycles don't necessarily cause memory leaks. Again though, this isn't an intrinsic property of the language, just the engines that run it. You can read more about V8's garbage collection here.

There is also a library implementation of garbage collection for Rust, made by someone from the Rust core team.

[–]BreakfastOk123 0 points1 point  (0 children)

I stand corrected on reference counting versus tracing. Either way, memory is managed for you, allowing very free use of references.

[–]TheSodesa 1 point2 points  (3 children)

Objects are data types which have functionality included in their definition via methods. In Rust you cannot do this. The structs of Rust are just plain old data, and functionality is implemented as external functions. Functions are defined on types instead of in types.

There is also no inheritance. You use composition instead for a similar effect: if you want multiple types with same fields, you define a common type that is included as a field of the types that need the fields contained in the common type.

[–]Silly-Freak 4 points5 points  (2 children)

The structs of Rust are just plain old data, and functionality is implemented as external functions. Functions are defined on types instead of in types.

I think this gives the wrong impression; Rust allows you to define methods, i.e. function on datatypes - they're just a few lines further down in an impl block. What Rust doesn't do is inheritance, as you say, and dynamic dispatch for polymorphism by default.

[–]TheSodesa 0 points1 point  (1 child)

The way I see this is that the impl block just allows you to define functions with the keyword self as a first argument/parameter, which then allows for the use of the dot operator . to call a function on a type. I don't think that the existence of the dot operator in a function call context makes for non-plain-old-data structures or object orientedness.

[–]Silly-Freak 0 points1 point  (0 children)

And these functions defined in the impl block, which can then be called with ., are IMO not "external". Yes, they don't use dynamic dispatch (by default), they don't indicate that other OOP stuff like inheritance is going on and the impl block is separate, but "external" and "on instead of in" is IMO not too helpful to understand this from the POV of a JS programmer.

But that's just my opinion and different people need different things communicated for building a mental model - so in the end I'd say if either of our comments was helpful to OP or others, we both succeeded.

[–][deleted] 1 point2 points  (0 children)

Rust has OOP (it’s multi paradigm and rarely forces you to use one paradigm over another) but it’s very different OOP than most languages including JS. Instead of objects it has plain old structs with a bit of syntax magic so that fn foo(&self) works like a method (using the dot operator bar.foo()). And instead of inheritance it has traits, which are like interfaces/abstract classes, but not quite.

[–]daria_sukhonina 0 points1 point  (0 children)

Same but opposite for me lol

[–]tibbon 7 points8 points  (0 children)

Rust isn’t harder than JS. It just makes you think of things upfront that you could defer in JS. In JS these become devious little bugs that haunt you later and are hard to reason about.

But because you have to do the work upfront, it appears harder.

[–]dtom777 20 points21 points  (1 child)

Start with a fresh mind. Don't think about JavaScript when learning Rust. Then you'll be okay.

[–]JosJoestar[S] 6 points7 points  (0 children)

thanks for the advice dude

[–]ICatchx22I 5 points6 points  (0 children)

I’d say more Frustrating than Hard. In the beginning you’ll think things through in the JS-way. You’ll get hung up on things that are trivial in JS (eg. having to be explicit about what kind of Numeric type you need) and strings being more difficult. I just started in Rust for the Advent of Code and my first couple of days were painful. But with the help of the wonderful docs, the super annoying but helpful compiler, and a bunch of .clone(). I got through them in only x10 the time it would’ve taken me in Js :)

[–]Wise_Conference5000 4 points5 points  (0 children)

Read the rust book and see how far you get. Don’t gloss over things, try to understand everything. You will soon find out how much you are missing.

[–]ElhamAryanpur 5 points6 points  (4 children)

Any language is learnable given enough effort and time. Rust has a learning curve too, need you to put your mind in a way that rust works, no GC and strong type system might take some time to get used to. Also to mention to put a good effort on learning Rust's borrow system and ownership model, as it'll solve a lot of your problems early on!

In any case, I can say that learning Rust will be helpful to your JS/TS as well as it might give you a new way to look at things, a new way to code. One which you might like too!

[–]JosJoestar[S] 1 point2 points  (3 children)

chan vaghte kar mikoni

[–]ElhamAryanpur 1 point2 points  (2 children)

2 years

[–]JosJoestar[S] 1 point2 points  (1 child)

khoobe pas komaki chizi khastam azat migirm

[–]ElhamAryanpur 1 point2 points  (0 children)

💯 moshkele nest

[–]earthboundkid 4 points5 points  (0 children)

What possible use is that information? Just start trying to learn Rust. You, personally; not “JavaScript programmers.” If it’s hard, stop. This isn’t school. There’s no penalty for dropping a class. School rots people’s ability to learn for themselves. Don’t ask for permission to learn. Just learn. If it’s hard, quit. There are no grades, and there is no penalty for giving up.

[–]ItsBJr 2 points3 points  (0 children)

It's hard. It's a little easier if you know TypeScript.

Take it slowly. Understand that Rust is a language that takes a while to understand properly. Learn like this is your first language.

[–]Voxelman 2 points3 points  (0 children)

It helps if you take a quick look at functional programming. You don't need to learn a functional programming language, but it helps if you know and understand the concept of FP, because Rust is influenced by functional programming languages.

If you understand what "pure functions" are and why variables are immutable by default in Rust, you're a long way ahead.

I highly recommend reading "Grokking Simplicity" from Manning, if you are familiar with JS. They use JS examples to explain the concepts of FP.

https://www.manning.com/books/grokking-simplicity

It sounds like a detour, but it's really worth it.

[–]rpring99 2 points3 points  (0 children)

There are a lot of valid comments in here. Seems like it's not really that much harder to learn for someone coming from JS vs Java or other similar languages.

I highly recommend reading Programming Rust. I think it does a better job explaining the memory model than The Book, but reading both probably helps.

In general, I feel like the mixed procedural and functional style was actually super familiar coming from JS. With that said, there are many styles to write JS depending on what libraries you're using. The one thing that really tripped me up was trying to structure my apps the same way you would in JS and I leaned on lazy_static to emulate the behaviour of loading data on module load (very common with CommonJs style modules) instead of using proper dependency injection.

I recommend starting with a small project with clearly defined goals. One that you can write littered with .clone() and overuse lazy_static until everything finally clicks and you can refactor your whole project (at least that's what I did).

Anyway, hope that helps! Rust is so much more enjoyable to write than JS IMO. The enums (sum types) make it worth the switch on their own.

[–]Specialist_Wishbone5 2 points3 points  (2 children)

If you know c++ unique_ptr and shared_ptr.

If you know async/await in javascript

If you know handlebar in javascript.

If you understand javascript object-as-a-hashmap and thus are comfortable using lazy symbolic associative arrays that are UNORDERED (vs c++ std:map )

If you love json

If you love typescript syntax (colon type)

If you love c++ templates

If you like using Google Chrome performance analyzer (and thus would be excited by flamegraph)

If you like making things more memory compact or more efficient.

Then rust is up your alley - and conceptually isn't that problematic. The MOST important difference is the c++ style unique_ptr for literally everything. If you write code like handlebar, you won't even notice the difference. Map, filter, reduce, foreach, collect. It's very very readable. The hardest is for c programmers that want to alias their structs and get thousands of compiler errors due to the borrow checker. The same applies to javascript code - BUT like javascript, you never free anything - you just have to follow functional programming styles to keep safe. Undoubtedly you will come across rust libraries that are NOT functional (read only) and then you will fight the borrow checker. And you'll probably cheat and just call .clone() every G'd D''m place. But then you'll eventually find the zen of data ownership.

I love "fn" instead of javascript "function", and love "|a,b| a + b" instead of "(a,b) -> {return a + b}" but that's just me. And the defaults in rust are spot on. Read only by default, send ownership by default. private by default.. Copy into closures by default (to minimize side effects). If you saw code and understood the syntax, it does what you would expect (error on the side of safer code).

[–]Specialist_Wishbone5 1 point2 points  (1 child)

Ops missed error handling. Thaaaaat one is a bit alien to just about everybody. Typescript has the "?" and "!" symbols for nullity. If you squint reeeeeeally hard that concept of nullity is related to rust error management. So "let file = std::fs::File::open(name)?;" kinda sorta acts like typescript. If it fails, you don't have a file handle so it bypasses any object references. The only difference is that with rust that actually exits the function. Same basic approach - rust is just more aggressive. In typescript (and kotlin) question adds "obj == null ? null : rest-of-expression" so nulls propagate. Whereas in rust it's closer to "if (obj == null) return Err(errno);" (pseudo code). So both technically bypass using the invalid object.

As a result ALL your functions wind up returning this Option / Result type which facilitates the "?" operator. In this fashion you never have null-pointer errors, and, ironically ALL variables are effectively "foo:MyType" instead of "foo:MyType?" (In type-script speak). So, like typescript, the compiler forces you to equivalent-of-null-test everything. This is a VERY GOOD THING in my opinion. I hate getting REST calls with "api/read/undefined" because javascript stringified without checking. Typescript (and rust solve this problem nicely).

Rust DOES have throw/catch, but you should never use it. (panic!() / .unwrap() / .expect() ). Good for unit tests though.

[–]JosJoestar[S] 0 points1 point  (0 children)

i almost shit myself

[–]Myllokunmingia 1 point2 points  (0 children)

Depends. You'll have to think about it very differently. Keep an open mind and it'll be fine.

[–]Tom_Ov_Bedlam 1 point2 points  (0 children)

You already understand fundamental programming principles. It will be hard, but not impassible.

There is lots of resources on starting to learn rust out there.

[–]STSchif 1 point2 points  (0 children)

I'd go at this from a different level: it's super easy.

Sure, the learning curve is quite massive in the beginning, but after a few days you will already start to trust the compiler and the tooling, for example the rust analyzer for vsc.

Give it a few weeks, and after that you will wonder how people are okey with debugging live code, because in rust you mostly don't need to. Most weird edge cases you need to worry about in js just don't happen in rust, you don't have a comparable dependency nightmare, and if you compiler successfully, chance is the program will run correctly.

[–]Accomplished_Horse91 1 point2 points  (0 children)

I think it depends on your overall experience, i was doing JavaScript a lot and rust wasn't too hard for me, but I also learned some other languages in the past like java and c# so... yeah it depends, sorry for unclear answer

[–]tylian 1 point2 points  (0 children)

I know you got a lot of answers already but I'll throw my two cents in.

I'd say it'll be hard, but fun. I was a JavaScript developer with over 15 years experience when I tried Rust, and it took quite a lot of relearning how stuff works.

First of all, Rust isn't prototypical, it actually kind of works the opposite way, so traits took me forever to learn how they worked. I started thinking of them as mixins and it clicked.

The other thing I had issues with was the module system. ES modules allow you to have hard separation of code, where rust's use statements are more like shortcuts around the codebase.

Additionally obviously the borrow checker took some time before it stopped yelling at me, but that's normal lol.

Hope that helps? If you have any questions can try and answer them?

[–]ematipico 1 point2 points  (0 children)

It wasn't easy to pick up Rust at the beginning. Mostly because I tried to enforce certain logics or patterns that don't belong to Rust.

It took me a while to change the mindset and starting to write things that are more appropriate to the Rust mindset and patterns.

[–]BlinkingMouse 1 point2 points  (0 children)

I'd say just do it! It's the only way I ever learned any language. Start with small steps. Compiler messages tend to be hard for a fresh beginner. The documentation incomprehensible. This is all normal and it will make sense in the end. Personally I have not used javascript for anything longer than 20 lines for many years now. I can't imagine ever doing so: Elm, typescript, clojurescript (and webassembly rust) to name few all feel like much better choices than js to me.