Is there a comparison site for rust to another language?


#1

Hello…

I’m a newbie to rust but have programmed in other languages (c, c++), i’ve read the book (the newer one), and have a bit of difficulty on the new terms but i fully understood the borrowing concept

I’ve been a strong believer that in order to understand more another new language, i want to see it side by side to another popular language to fully understand it…

do you know of another site that have this kind of comparison? i mean not the basic stuff, but the more advanced ones? like…

  1. how to define a global variable (if possible)
  2. how to dynamically allocate an object (struct in rust) and being able to control when to destroy it (allocate to a vector perhaps) (if possible)
  3. pointers (if possible in safe rust)

etc.

i really want to learn rust, but sadly it has a steep learning curve, even for an experienced programmer, rust aint easy, i feel that the documentation (the book) can benefit if there are some comparisons to other language to facilitate the transition (like match is a c/c++ switch statement, closures are similar to lambdas, etc)

i know rust is powerful (hence i want to learn it), i know i have to bend my thinking a little bit (what i used in other languages, i can’t do in rust), i want to learn it, i’ve read the book several times but sometimes the new technical jargon, i don’t fully get it…

also I just wanted to ask this, I want to learn rust to port my c++ program to rust (to learn so i understood the concepts), is it possible to save state in rust? literally create an object dynamically which saves state…(sample socket server app), i open a socket and i need to save some state, that will have a lifetime that of the client?


#2

Someone had linked https://stevedonovan.github.io/rust-gentle-intro/readme.html before, saying they found it really helpful for a person with a C++ background. I’ve not read this myself, so YMMV.


#3

wow, this is a great link…


#4

To answer some of your questions:

Yes, it’s possible although. Depending on the type of the variable and how you plan on using it, there are several options:

  1. A static binding - this requires the type to be constructable statically, and with current Rust’s limited const fn support, this is basically POD types with no heap allocation internally.
  2. Use the lazy_static crate - this lets you do arbitrary initialization.
  3. Use thread_local that gives you a (surprise!) thread local global.

There are some other caveats with statics/globals, and generally you’ll want to avoid them in Rust. If you have a specific case in mind, we can use that to elaborate.

Box<T> will allocate T on the heap. For example, let boxed_i32 = Box::new(5) gives you a heap allocated i32. It gets destroyed when the Box itself is destroyed. This is like unique_ptr in C++.

You can create raw pointers in safe Rust, but to dereference them requires unsafe code. Generally you’ll use references if you want the compiler to ensure memory safety.

Absolutely possible or else Rust would be useless :slight_smile:. If you have a bit more specifics, we can elaborate.


#5

thank you for the info, will go deeper into the rabbit hole…i really need to read the documentation more (and been also watching some tutorial vids)


#6

This is a great problem you bring up, although I’m unsure of a solution. I’m a self-taught guy and I had only a very high-level understanding of the tools I used (mainly Ruby). Rust introduced concepts I’ve never heard before, so I couldn’t really do much comparison. Rust is fairly different in many ways, especially without prior knowledge of other languages descended from ML. Enums really tripped me up when I first started.

I’m not sure if there’s a great way to compare without the reader understanding those concepts in the first place.


#7

You’re right, I’m also a self taught guy, in a traditional sense of programming, I find that the borrowing schemantics of rust is the only one being different (as far as i know) from other languages, and this creates problems when passing data through functions, multiple threads, which your used to do in other languages…In rust you need to know the flow of data such that only one is owner…(correct me if I’m wrong)