From #JavaScript / #Typescript to #rustlang: A thread
doc.rust-lang.org/stable/book/fo…
I haven't dealt with dependencies yet, I hope that's not as messy as on Node 🙏🏻
Does @jetbrains have plans for a Rust IDE?
`...` would have been much more natural. Is that another operator? Does Rust have something like js' spread operator?
I haven't made up my mind on how this compare to inheritance-based languages. I worked ~9 months with Go, and I don't remember missing classes there. We'll see.
I hope that's a first-class construction. It doesn't seem to be a macro, as those are applied with `!`.
I guess the order makes sense, it emphasises the new values.
I'd love to be able to do this `let point = { x: 10.3, y: 0.4 };` instead of `let point: Point = { x: 10.3, y: 0.4 };`. I wouldn't be saving a single keystroke though.
It's almost the same syntax than literals, so maybe that's the reason for the limitation mentioned in the previous tweet.
#![allow(dead_code)]
#![allow(unused_variables)]
Thanks @snfernandez
You probably use `?` all the time in real programs, so idk.
But what if it evaluated the arguments in order? Is evaluation order defined? I should google it, but I'm afraid to end up in a 🐇🕳
Macros being hard to debug is my biggest fear about rust. I hope I can see the result of expanding it somehow.
Do people pass arguments using & most of the time? I get why you'd do it, but I was expecting that to be automatic.
It took a while to install, and then it failed because I'm not on nightly. Do people use the nightly build of their compilers? 🤯
Now I do `let i:i32 = smth` and read the error message 😅
You create an `impl`ementation block and define functions that take self as their first argument.
Most linters from other languages will complain about shadowing, but it's common practice in #rustlang.
There's also C-like enums. Those just have "integer" values, and are defined in the same way as the fancy ones.
I don't have enough experience outside of x86 and modern android and ios development, so I was expecting a language this new to default to i64, or maybe to the word size.
Any clue?
You probably want to use `&self`, right?
I know there are other kinds of smart pointers, and also "raw pointers". Will get back to this soon, I guess.
doc.rust-lang.org/stable/rust-by…
let list = List::new();
let list = list.prepend(1);
let list = list.prepend(2);
I know very little about lifetimes, so I don't get it yet. I guess that's why the book doesn't explain it.
`const` means "this is immutable, feel free to inline the value and save some memory".
When in doubt, you should probably use `const`. (4/4)
This works:
let v;
// ...
v = 1;
They probably don't, you have really powerful enums for that.
You know what they say: Rust wasn't built in 10 days.
patriciopalladino.com/blog/2012/08/0…
They are definitely not just reinterpreting the memory as the new type.
If TS were like Rust, the set here would be a `Set<number>`, instead of a `Set<unknown>`:
let m = new Set();
m.add(123);
Impressive!
I'm starting to see a pattern with "try" being used as "this thing may fail".
I'd probably use an enum for that, so you can discern between errors.
Everything has a message about it being "nightly-only". That's so confusing. Shouldn't the official docs point to the stable version?
I know what that means, but I have a CS degree. This must be frustrating for many ppl.
It was very fun doing this. I'm moving slower, but I think I'm still on track with my goal.
See you tomorrow 👋🏻
I only have a couple of hours, so I won't be tweeting as much as yesterday.
I'll try to reply to all of them, but I may not be able to do it if I want to keep making progress.
Makes sense. I guess I'd be working mostly with String.
Well, I have to learn about "procedural macros" first. Aren't all macros procedural? Can I hang the compiler with a macro?
I think Ruby has .. and ...
You can use it through `?`, but you can't implement it because it is not finalized, and may change.
It's an interesting approach, just a little confusing for newcomers.
Someone recommended using different crates (libraries) for errors in applications vs libraries.
Never saw this idea before, but as errors are just values that you explicitly return, you can do that.
I normally wouldn't care about this. I believe the evaluation order of args is undefined in most langs, but I think the borrow checker makes it relevant.
Are they related to typestates?
There are native assertion macros. That's interesting, but I bet people end up using libraries for that.
It's when you specify types like this `f::<i32>()`.
Most languages don't require the `::` for that. I guess omitting those would lead to some syntax ambiguity in rust.
If the last line of your function doesn't have a `;`, then, it's an expression, and it is used as the return value.
If you add a `;`, it's a statement, and a `()` is returned instead.
I saw this in another lang. Maybe rust many years ago?
If an unwrap fails, it "panics". I guess a panic is an unrecoverable error that crashes your program, like in go.
Did I mention that I'm a huge PL nerd? 🤓
You must ALWAYS use curly braces, like an adult.
`if`s are also expressions 🤩
Or maybe it's the vscode integration?
Not sure if it makes a huge difference, but it's cute anyway.
I've never used that in Java, and worked with it for years.
Hope they aren't common in Rust.
Matching on numbers is incredibly powerful. e.g. `13..=19 => println!("A teen")`
Can I do that with my own types? 🙏🏻
I know that `&` is automatically inserted sometimes, but that makes it even more confusing.
Also, there's `ref` and `&`?
`=` would be confusing though.
How is that scoped? Are those implementations global? Or should I import them to reuse them from other modules? I wish it's the latter.
There's an example in the book, but I'm not convinced.