Post AczoIl3Thqlot3fO6a by alexelcu@social.alexn.org
(DIR) More posts by alexelcu@social.alexn.org
(DIR) Post #AczWEf8xwdYuDg8B8a by NicolasRinaudo@functional.cafe
2023-12-18T21:19:17Z
0 likes, 0 repeats
Slightly confused by something very silly - ; in rust. My current model is, it's used to separate statements. This seems simple enough, but the bit that I find hard to get used to is, since it separates statements, your last statement cannot be followed by a ;, else it means a statement comes after - which rustc will infer to be (), apparently?
(DIR) Post #AczWEnQz7WjFvscgKm by kubukoz@mstdn.party
2023-12-19T00:28:33Z
0 likes, 0 repeats
@NicolasRinaudo it's annoying. If you have semicolons you might as well allow trailing ones.
(DIR) Post #AczWEoSRJbUF6gJMEy by NicolasRinaudo@functional.cafe
2023-12-18T21:31:41Z
0 likes, 0 repeats
It doesn't help that the rust book seems to finish all of its unit-returning blocks with a ;. Aren't they optional? Or is that a convention?Is there anything I can read on the subject? I know it's a weird thing to get stuck on, but I don't like it when things I should understand keep behaving in ways I don't expect.
(DIR) Post #AczWEoVd7k2TGZnuDI by NicolasRinaudo@functional.cafe
2023-12-19T22:10:30Z
0 likes, 0 repeats
@kubukoz no they're allowed, just not *always* necessary, and that's the bit that I struggle to get my head around
(DIR) Post #AczWEpt406NTXXm8NU by kubukoz@mstdn.party
2023-12-20T01:51:49Z
0 likes, 0 repeats
@NicolasRinaudo IIUC, you can't put a semicolon after a value you mean to return.```fn foo() -> u8 { 10;}```doesn't typecheck. So you can't do trailing semicolons in the same meaning as, say, trailing commas in Scala.
(DIR) Post #AczWErA7GBc1UilGb2 by alexelcu@social.alexn.org
2023-12-20T05:41:06Z
0 likes, 0 repeats
@kubukoz @NicolasRinaudo You can think of semicolons in Rust as having type Unit. So if you add one last semicolon, the type of the final expression being returned is Unit.
(DIR) Post #AczWRABwKpQrzKTzs0 by NicolasRinaudo@functional.cafe
2023-12-20T05:43:21Z
0 likes, 0 repeats
@alexelcu @kubukoz but then why the convention of adding one after the last expression in a block, if that last expression evaluates to unit?It can’t be for consistency’s sake, since you must not add one if your last expression does not evaluate to unit.
(DIR) Post #AczX50k2tALQGNR6x6 by alexelcu@social.alexn.org
2023-12-20T05:50:37Z
0 likes, 0 repeats
@NicolasRinaudo @kubukoz Adding superfluous semicolons doesn't generate a compiler error, and if there's a convention, it's probably for the same reason that people in Scala now like to add colons after the last element of a list, even if not necessary.
(DIR) Post #AczXB3HIN7AJrnrwhc by NicolasRinaudo@functional.cafe
2023-12-20T05:51:43Z
0 likes, 0 repeats
@alexelcu @kubukoz i would get that, if it wasn’t sometimes a type error. This is the part that bothers me, the inconsistency.
(DIR) Post #AczXWpAZVVvwTt6deK by alexelcu@social.alexn.org
2023-12-20T05:55:38Z
0 likes, 0 repeats
@NicolasRinaudo Can you give an example? I might know what you mean, but not sure.@kubukoz
(DIR) Post #AczXjCQiqpag65YroG by NicolasRinaudo@functional.cafe
2023-12-20T05:57:51Z
0 likes, 0 repeats
@alexelcu @kubukoz sure - if you write a function that returns,say, u32, and your last expression terminates with a semicolon, that’s a type error. The compiler will reject it, because you’re trying to return unit.So for functions, you must not use trailing semicolons. For procedures, you should by convention.
(DIR) Post #AczZ5zGC59mnrm4Bkm by alexelcu@social.alexn.org
2023-12-20T06:13:12Z
0 likes, 0 repeats
@NicolasRinaudo I don't think I find this part confusing. E.g., this is legal, with, or without the semicolon (due to last expression being Unit):fn hello() { println!("Hello, world!")}This isn't because Rust does not automatically convert to Unit:fn hello() { println!("Hello, world!"); 1}But this is fine because the semicolon turns that last expression to Unit:fn hello() { println!("Hello, world!"); 1;}Do you mean something else?@kubukoz
(DIR) Post #AczZJQ9xNkkd3eYTBI by NicolasRinaudo@functional.cafe
2023-12-20T06:15:35Z
0 likes, 0 repeats
@alexelcu @kubukoz no i mean exactly that. And i do find it confusing, that you sometimes can use a semicolon, sometimes should , and sometimes absolutely must not, depending on the return type of your function’s type. I’ll get used to it, but it feels inconsistent at the moment
(DIR) Post #AczZqwHwwrr8GCHC8e by alexelcu@social.alexn.org
2023-12-20T06:21:41Z
0 likes, 0 repeats
@NicolasRinaudo The way I think of it is the compiler does not trigger errors for superfluous semicolons, although it might give you a warning if there's more than one:fn hello2() { hello();;;;;;;;;}Btw, I also thought you were thinking of `let` expressions, which do require a semicolon:fn hello() { let x = 5;}Well, the good thing is that the compiler is precise in telling us where a semicolon was needed, probably because it could infer most of them.@kubukoz
(DIR) Post #AcziKsp3CPR7paY3rE by NicolasRinaudo@functional.cafe
2023-12-20T07:56:42Z
0 likes, 0 repeats
@alexelcu @kubukoz I understand the compiler's behaviour given the constraint it has, yes, and given these constraints it makes sense.I just don't really understand the constraints. The fact that, depending on the return type of your function, you should (but don't have to!) or absolutely must not use a semicolon to terminate your last expression feels counter intuitive to me.I'm not saying it is in general, maybe absolutely everybody in the entire world finds this perfectly natural and reasonable. Maybe I will, too, with practice! Just, at this point in time, I find that design decision hard to understand and would love a concrete reason for it. It feels like there must be one.
(DIR) Post #AczoIl3Thqlot3fO6a by alexelcu@social.alexn.org
2023-12-20T09:03:34Z
0 likes, 0 repeats
@NicolasRinaudo Yeah, OK, I understand. And the semantic is definitely different from Scala's.@kubukoz
(DIR) Post #Aczrj3ZoixblLaZGWu by kubukoz@mstdn.party
2023-12-20T09:41:56Z
0 likes, 0 repeats
@alexelcu @NicolasRinaudo forget Scala, the semantic is different from any other language with semicolons I've seen.