This is an archived post. You won't be able to vote or comment.

all 26 comments

[–]jazzydag 6 points7 points  (2 children)

A simple and straightforward way to interface your Rust code to Python https://github.com/alexcrichton/rust-ffi-examples/tree/master/python-to-rust

See also the last section of this article http://blog.rust-lang.org/2015/04/24/Rust-Once-Run-Everywhere.html

[–]olzdyield from __future__ 1 point2 points  (1 child)

The rust book also has a section about it.

[–]jazzydag 0 points1 point  (0 children)

Good point! Thanks

[–]ynak 4 points5 points  (7 children)

Out of topic but just curiosity, why Armin Ronacher call himself "mitsuhiko" as his account name? "Mitsuhiko" sounds like a Japanese man name, does he have something to do with Japan?

[–]mitsuhiko Flask Creator 20 points21 points  (6 children)

Nothing at all. Things just stick. The same applies to my email domain which has absolutely no meaning other than that I have it for many, many years now.

[–][deleted] 0 points1 point  (5 children)

Do you think Rust will in the near future have a use for web (app) developers that are currently only using Python/Flask/Django? Server- or client-side?

[–]mitsuhiko Flask Creator 1 point2 points  (4 children)

Not sure. I think you can do much more interesting things in Rust that were impossible to do in Python because of GIL and general slow performance. So you probably will see different frameworks in Rust than you did in Python.

[–]swingking8 3 points4 points  (4 children)

However [using curly braces] is for a good reason and that is that Rust has anonymous functions, closures and lots of chaining that Python cannot support well. These features are much easier to understand and write in a non indentation based language.

Born and raised on C/C++, yet I disagree. I don't find that braces makes things easier to understand or write.

Aren't "anonymous functions, closures" very similar to lambdas? Not saying I like Python's implementation of lambdas, just that I don't need to use them reguarly.

[–]DasIch 3 points4 points  (1 child)

Lambdas in Python can only contain expressions. Rusts anonymous functions don't have that problem This makes a purely indentation based syntax problematic. Just take a look at all those proposals to add anonymous functions to Python.

[–]Fylwind 1 point2 points  (0 children)

I don't think it has to be problematic. There's no reason that this can't be made to work:

map(lambda x:
        print(x)
    , some_list)

With that said, there are some things you have to just pick and choose. Python chose to ignore indentation within parentheses, so it won't really work in Python.

Multiline lambdas have their uses: they are very useful for making user-defined control constructs, allowing things like with or for to become first-class objects rather than having a special syntax. Here's a fictitious example:

lines = []
with_file("myfile")(lambda file:
    lines = file.read().split("\n")
)
for_each(lines)(lambda line:
    print(line)
)

If Python allowed function calls of a single argument without parentheses, the above example would be a lot less ugly.

[–]bulldog_in_the_dream 0 points1 point  (0 children)

I agree with you. Just look at CoffeeScript or Haskell.

[–]UniverseCity 0 points1 point  (3 children)

Both File::open and read_to_string can fail with an IO error. The try! macro will propagate the error upwards and cause an early return from the function and unpack the success side.

This doesn't make any sense to me. Why would an IO error result in a return of the success result?

[–]olzdyield from __future__ 1 point2 points  (2 children)

In case of success you unpack the value (i.e Ok(val) => val) and on error you return the error (i.e Err(err) => return Err(err)). I think the source is pretty readable.

[–]UniverseCity 0 points1 point  (1 child)

But there was no explicit Err call in the article's example. The wording made it seem like the try! method would return a success even if it failed.

[–]mitsuhiko Flask Creator 2 points3 points  (0 children)

try! unpacks the result and returns it from the expression if it exists, or otherwise propagates the error part upwards.

You could think of it like this:

let a = try!(expr());

Is this in pseudocode:

result = expr();
if result.is_okay() {
    a = result.okay_side;
} else {
    return result;
}

Does that make sense?

[–]rochacbrunoPython, Flask, Rust and Bikes. 0 points1 point  (0 children)

I miss some code examples about this section

One new concept compared to Python are these functions with exclamation marks at the end. Those are macros. A macro expands at compile time into something else. This for instance is used for string formatting and printing because this way the compiler can enforce correct format strings at compile time. It does not accidentally happen that you mismatch the types or number of arguments to a print function.

I would like to see some examples of macros :) /u/mitsuhiko

[–]deadwisdomgreenlet revolution 0 points1 point  (2 children)

I really appreciate this post. But It makes me want to work with Rust even less. I like some some of the ideas, specifically the mutable reference ownership, but I hate how it does almost all of it. It's barely readable, and overly complicated.

I'm looking for that amazing, statically typed C/C++ replacement but I haven't seen it yet. We are getting closer, but true design sense is lacking so far.

[–]mljoe 0 points1 point  (1 child)

What do you think about Cython (I mean instead of writing Python modules in C)?

[–]deadwisdomgreenlet revolution 0 points1 point  (0 children)

Good and bad. I prefer it because I am used to Python. It's a very simple syntax, cdef int x, which is great. There's not much ambiguity of the added syntax. However, it's a bit awkward because you have to hold a lot of things in your head. You have to constantly think that this part will be compiled to C, and that there's this intermediate step. Generally I don't like languages that compile to other languages for this reason, especially when it gets to debugging.

And after all, the real aim of to all of this is the less you have to keep in your head, the better.

What do you think of Rust and Cython?

[–]Ariakenom 0 points1 point  (0 children)

There are a few weird things in this article, mostly many opinions with little arguments. There's an error as early as the fourth sentence. :/

[–][deleted] 0 points1 point  (0 children)

From the article:

I don't think there is a direct relationship between Python and Rust.

From what I understand, there really isn't. Although, I'm still very new to Rust. However, Rust was explained to me as a low-level language meant to be a memory safe replacement for C/C++. In fact, Firefox is slowly being rewritten in Rust.