
Exploring Rust (from C#) - nercury
http://nblumhardt.com/2016/03/exploring-rust/
======
taspeotis
The annotated version of C# included in the blog post reminds me of Spec#
[http://research.microsoft.com/en-
us/projects/specsharp/](http://research.microsoft.com/en-
us/projects/specsharp/)

Spec# has annotations and methods for object ownership. You can see an example
of it in the "The Spec# Programming System: An Overview" slideshow (slide 39
onwards).

Also:

> C# has two families of data structures that dermine allocation behaviour:
> structs and classes. If a type is a struct then values of that type will be
> allocated on the stack:

Here's a primer on how the Microsoft CLR handles it, because it's more subtle
than what's stated above. The end effect is that value types behave like
values, reference types behave like references. Where they're stored is a
function of where they need to be stored, not their type.

[https://blogs.msdn.microsoft.com/ericlippert/2010/09/30/the-...](https://blogs.msdn.microsoft.com/ericlippert/2010/09/30/the-
truth-about-value-types/)

~~~
nblumhardt
Ah yes, my bad - the "value types go on the stack" simplification is a bit
sloppy. It's a useful way to describe it to make the comparison - but
definitely not for understanding C# in this case.

~~~
taspeotis
All good. Also, thanks for alt-composition and stateless.

------
kibwen
I like the author's attempt to explain Rust's borrow checker to a new audience
by inventing a variant of C#. Lifetimes are certainly one of Rust's most
unique features, and the existence of analogies to express unfamiliar concepts
in terms of familiar ones is very valuable.

~~~
gravypod
What I like about the concept is that as soon as you explain it, the first
response you see is "Wait, that doesn't already happen in other languages?"

The idea just makes sense, it's "right".

------
FranOntanaya
Hug of death. Cache
[https://webcache.googleusercontent.com/search?q=cache:EDeYMv...](https://webcache.googleusercontent.com/search?q=cache:EDeYMvVfqygJ:nblumhardt.com/2016/03/exploring-
rust/)

~~~
h43k3r
This is offtopic but what should be size of the VM to handle HN's front page
traffic. I know a lot of it depends upon whether the website is static or is
backed by DB and on various different factors. But many a times some of my
friends ask me questions like this.

To make it simple, lets take 2 very basic websites.

1\. A ghost blog. (node.js) 2\. A wordpress blog backed by a mysql db. (php)

~~~
mseri
I had a ghost blog on a micro instance of a german VPN, a machine with 256mb
of ram and not too fast processing power. Both times I reached HN home page,
in one case for some hours, it worked without issues and only a minor
slowdown.

~~~
mseri
VPN was meant to be VPS but I cannot edit the post

------
qz_
Could someone explain why Rust is so damn popular/controversial?

~~~
skoczymroczny
I think a lot of people are looking for a replacement for C++ that isn't
C#/Java. As in a language that is still low-level enough to offer stuff like
pointers, but without the pitfalls and minefield which is C++ (a lot of which
is due to historic reasons and backwards compatibility with C).

Some newer languages that try to fill the niche are D, Nim, Rust and Go. Can't
say much about Nim, it seems to be in the background. Go started as a systems
programming language, but since then they backed out on it and it's mostly
pushed as nicer C and webservice language. That leaves D and Rust on the
field. Both are competing for the same audience, but Rust has the advantage of
a popular brand backing (Mozilla) and a potential killer app (Servo, Mozilla's
new work in progress browser engine). It also seems to have a clearer vision
because of Mozilla backing and constant dogfooding in Servo. Meanwhile D is
more of a design by community, as a result it gets not always the features
that are needed or people want, but the features people are willing to work
on. Also, D gets a lot of backlash from the game dev and system programming
languages communities for using a (not too great) garbage collector, whereas
Rust uses memory management somewhat similar to C++ model with RAII/smart
pointers, however greatly improved and with much stronger static checks.

I think people just view Rust as the language that could actually be the
alternative to C++, because there is a place for a language that is safer to
use than C++, yet performs at similar speeds and doesn't handhold you like
C#/Java do.

~~~
gjm11
> Some newer languages that try to fill the niche are D, Nim, Rust and Go.
> Can't say much about Nim, it seems to be in the background.

Nim is frustrating, because to my taste it gets so many things just right
while getting one particular thing so spectacularly wrong that I can't bring
myself even to try it.

The one thing is its rule for when two identifiers are the same. They are
compared case-insensitively, ignoring any underscores and en-dashes in either
name, except that the first character must match exactly.

Consequence #1: if you have, say, something called user_sort (perhaps it's a
function that sorts users) and something called use_RSort (perhaps it selects
which of two sorting algorithms to use somewhere) then they will be _the same
identifier_ , with results anywhere from compilation failure to silent
malfunction you don't notice until a year later.

Consequence #2: if you want to search for an identifier using some obscure
tool like, say, grep or Vim or Eclipse, you're out of luck. (You can make your
own tool -- call it "nimgrep", perhaps -- and extensible things like Vim and
Eclipse can be given the ability to search for Nim identifiers. But their
built-in facilities, like what "*" does in Vim, will do the wrong thing if you
are working on Nim code.)

~~~
progman
> Nim is frustrating

I have used Nim heavily for more than a year now, and it turned out to be the
most productive language I have every used. Nim could be considered a modern
Lisp with infix notation and Python like syntax with native C performance. The
macro system is incredibly well designed and easy to handle.

I also tried Rust, and it turned out to be much more complicated and time
consuming. I would use it for systems programming only. Redox is the only
reason for me to learn Rust.

> The one thing is its rule for when two identifiers are the same.

First it looked strange to me as well. However there are actually good reasons
to avoid case sensitivity.

[http://blog.codinghorror.com/the-case-for-case-
insensitivity...](http://blog.codinghorror.com/the-case-for-case-
insensitivity/)

As for the user_sort and use_rsort example, such semantic errors can easily be
fixed by qualification (a.user_sort and b.use_rsort). Errors like the
following one however are very hard to fix in case sensitive languages like
C++ and probably even Rust:

    
    
      module A:  func smart_func (x,y) = do_this
      module B:  func smart_Func (x,y) = do_that
      module C:  func Smart_Func (x,y) = do_something_else
    

main:

    
    
      import A, B, C
      smart_func (a,b)  
    

hmm ... smart_func(a,b) doesn't work. Seems to be a typo. Which function did
the author really mean?

Nim would reject such a mess in general.

The developers of Nim have implemented a lot of nice features. A really silly
idea however are strong spaces. No serious developer would ever use such a
dangerous "feature". It should be removed from Nim.

[https://github.com/nim-lang/Nim/wiki/Whitespace-FAQ](https://github.com/nim-
lang/Nim/wiki/Whitespace-FAQ)

~~~
kibwen

      > Errors like the following one however are very hard to 
      > fix in case sensitive languages like C++ and probably 
      > even Rust
    

No, in Rust the compiler will emit a style warning if you have a function
whose name includes capital letters. This can be toggled off (and also
elevated into a hard error), but it's always on by default.

Furthermore, when you import a module Rust keeps it namespaced under the
module's name, so you never need to worry about names accidentally colliding
or being unsure as to where a symbol comes from. You have to use an explicit
glob import to pull in all of a module's public items into the current
namespace.

Furtherfurthermore, Rust warns when you import a symbol and then don't use it,
so even if you name all of these functions differently _and_ ignore the style
warnings _and_ glob-import the modules, then you'll get yet another warning
when you fail to ever use two of the symbols.

Furtherfurtherfurthermore, Rust doesn't allow symbols with the same name to
exist in the same namespace, so even if you went back and fixed all those
functions to comply with the style warnings, you'd get compilation errors if
you continued to glob-import any of the two of them.

------
agentgt
Hopefully this is relevant but one of the things I had difficulty with in Rust
is closures (or more specifically the lifetime and pointers of closures) and
wiring up a system or library that needs lots of callback things.

See I'm used to Java where anonymous classes are closures (I really like that
about Java/Scala/Groovy. One of the very few things I don't like about C# is
that does not have anonymous classes).

So the problem is when I want a callbacky thing that has state it gets fairly
complicated in Rust because you have lots of options that have limitations.
For example there is bare functions or raw functions which basically
stateless, there is traits but you'll need to use boxes and or cells, and then
there is Rust closures which I had some various issues with because of bugs
(they are probably fixed now).

I only bring this up because instead of so many tutorials on simple static
programming stuff I would like to see how one might wire up a system with many
callback components (ie manual dependency injection). I suppose I should look
at how its done in Iron.

ie if your going to show something analogous to a Java/C# developer show them
some wiring up of a complicated system that has interfaces and the components
can be interchanged (perhaps that is just something you don't do in Rust but
I'm curious how it would be done).

~~~
pbz
> One of the very few things I don't like about C# is that does not have
> anonymous classes

Anonymous classes are lambdas in C#.

[http://programmers.stackexchange.com/questions/195081/is-
a-l...](http://programmers.stackexchange.com/questions/195081/is-a-lambda-
expression-something-more-than-an-anonymous-inner-class-with-a-singl)

~~~
agentgt
Not all anonymous classes are lambdas.

There are many situations like async streams or even synchronous iterators
that require multiple methods.

Erik Meijer even mentions somewhere in this video that he is remiss that C#
doesn't have anonymous classes: [https://channel9.msdn.com/Events/Lang-
NEXT/Lang-NEXT-2014/Ke...](https://channel9.msdn.com/Events/Lang-NEXT/Lang-
NEXT-2014/Keynote-Duality)

Around 47:50 he says "feature request for C# anonymous inner classes".

------
maxxxxx
Does anybody know how good the Rust integration with Windows is? The language
looks very good but can you interop with Win32, COM and .NET? Is there a
debugger? Without those its usability is a little limited.

~~~
kazagistar
Haven't used any yet, but there are number of libraries.

Generated winapi bindings:
[https://retep998.github.io/doc/winapi/](https://retep998.github.io/doc/winapi/)

A COM library, including a macro for generating a COM interface:
[http://eljay.github.io/com-rs/com_rs/index.html](http://eljay.github.io/com-
rs/com_rs/index.html)

Not exactly sure about .Net, but here is a tutorial for how to use a rust
library from C#: [http://www.loekvandenouweland.com/content/using-rust-code-
fr...](http://www.loekvandenouweland.com/content/using-rust-code-from-csharp)

------
jld89
Error establishing a database connection...

