Python-like syntax for Rust

im coming from python (like most people here) and im constantly disappointed by how the code looks and how it behaves. i obviously talk about the use of : '{' '}' and ';'
is there a way to change the theme to look like python (without {};)?
I believe that the compiler (with few adjustments) can translate the code the same ether way so i dont see why not.

I am not. I know Python but I program Java for a living, and use many other programing lanuganges.

If you want to program Python, use Python.

5 Likes

I would not be so certain that most people come to Rust from Python. There are a lot of languages out there to come from: C, C++, C#, Java, Javascript, Ruby, and so on and so on.

Using white space indentation for block delimiting is not at all a good idea. Those curly braces are there for a reason. As are other similar block delimeters in almost all languages. As are the semi-colons.

For example did you know that a statement with a semi-colon at the end has a very different meaning than one without in Rust?

I am very happy that Rust does not look like Python.

7 Likes
3 Likes

Ha, love the README in the slag repository:

Should I use this

No. Use the default rust syntax.

3 Likes

IMO, Rust has features that Python couldn't support because of indentation way.
For example, Rust has closures, Python only has one-line lambdas.

2 Likes

This reminds me of how people going from pascal to C back in the day sometimes defined BEGIN and END to { and }, and then started trying to find other creative ways to make the C code look more like pascal using the preprocessor.

They all stopped doing that eventually when they realized there's a good reason for C to look like C, and not pascal.

2 Likes
let a = match b {
      Some(val) => {
           println!("{}", val);
           val
      },
      None => 0
}

You could write this without the semicolon or the parenthesis but it wouldn't exactly look nice. I don't think anyone really cares for it, since other than a few outliers most languages still use parenthesis and semicolons, and frankly in my opinion it seems like putting effort into making a language work without them is a lot of wasted effort. I don't want autoformatting to break because of ambiguity caused by indentation.

1 Like

Quite so.

And once code snippets have been cut'n'pasted around the net an the whitespace consequently messed up it's not obvious how to fix it.

Refactoring code becomes a lot more delicate.

Having seen the huge number of people turn up to the Raspberry Pi forums asking why their Python code does not work, only to find it's an invisible white space issue. I can only conclude it is a really bad idea.

I just refactor or rewrite old stuff by copy pasting and typing willy nilly and let good old ctrl + s figure things out, and wonder how I've ever lived without autoformatting up till now :sob:

1 Like

Just remember that there are trade offs for almost every discussion. There are good reasons for Python's syntax and there are good reasons for Rust syntax. No good thing about Python's syntax is that a program has to be well reasonably well formatted to run. This is especially good for new programmers because it forces them to indent properly. When I was in school, I remember reading some of the other student's Java programs were each line of a function would have a different indention level. Very hard to read.

But I would also say that a language having multiple alternative syntaxes would be a bad idea because it would mean that you would have to fully understand both to work in the language. Imagine asking a question and have people's answers containing two very different syntaxes. I would make learning the language and getting help very hard.

2 Likes

Sorry, as many here it's not my case.

I think instead of asking about syntax of c-like languages, you should rather ask yourself what made you look outside python, and why Rust among other compiled languages. Then, ask you if it's worth the effort to use bracket delimiters :slight_smile:

2 Likes

Maybe you would like to give inline-python a try:

From the README:

use inline_python::python;

fn main() {
    let who = "world";
    let n = 5;
    python! {
        for i in range('n):
            print(i, "Hello", 'who)
        print("Goodbye")
    }
}
2 Likes

I actually do come from Python, but I have a lot of experience with Python and JavaScript and other languages, too.

One thing I've found is that seemingly simple questions such as, "why couldn't the compile translate a whitespace-based syntax?" are often much more complicated then they first seem. ( But don't hesitate to ask them! Ask questions. It's good to ask questions. :+1: )

I've seen this with question after question about improvements to the Rust language. Someone asks an innocent question about, "why can't we have feature X?" and I'm thinking, "oh yeah, why not?', and then a ton of really smart people come on and say "well you have to consider this situation, and this situation, and this situation..." and then I step back and I'm like "wow, that was way more involved than I thought it would be".


Anyway, you have to consider that many of Rust's design decisions are founded on the guarantees that are afforded by it's syntax and that it wouldn't be able to have the same functionality, at least in some situations, but probably a lot of situations, if it had a similar syntax to Python.

:+1:

I agree. The syntax of Rust is not going to change that much, so with that understanding, what makes Rust worth it to you?

3 Likes

There is a lot I could complain about with the Rust syntax.

When I see code posted here that is full of generic angle brackets, life time tick marks, trait bounds and the like it is just so much random looking "line noise" I wonder how it could ever convey any meaning to anyone. My eyes glaze over, my visual cortex crashes, it does not make even make it far enough into my brain for further analysis.

But of course I could not begin to suggest how else these things might be achieved in a more legible form. Disregarding the fact that it's not going to change now.

So we just have to read it and write it long enough that it soaks in naturally and some fluency is achieved.

Luckily my application level code is almost entirely free of all that and looks quite nice after it has been clippied and fmt'ed. :slight_smile: Which is good because I want my non-rust fluent colleagues to at least have some chance of understanding what I wrote. I would not want to scare them away.

What I never did understand about Python is: How come, after doing such a good job of expunging all those brackets and braces and "redundant" punctuation with a clean, line based, white space delimited syntax does it still need those pesky colons in function definitions, after "if" statements etc? They seem totally redundant and inconsistent and stick out like sore thumbs.

1 Like

Yes, practical Rust often does not need many of those more "violent" syntax elements, thankfully. I think that is something that was improved with the Rust 2018 edition as they found out what wasn't needed in the vast majority of cases ( such as lifetime ticks in functions that take/return references ).

That's what's great about Rust is that you can often leave the "dark magic" that is necessary sometimes to the people who write creates that abstract over all those techniques that many of us would prefer to avoid. :slight_smile:

1 Like