rust一键开服
I have been programming with Rust for quite a long time now but that does not mean much. Rust has been changing for years now in such dramatic ways that coming back after two months feels almost like working in a different language. One thing however never changed: the trajectory. With every update, with every modification the whole thing became better and better.
我已经使用Rust编程很长时间了,但这并不意味着什么。 Rust多年来一直以如此戏剧性的方式发生着变化,以至于两个月后回来似乎就像在使用另一种语言。 然而,一件事从未改变:轨迹。 随着每一次更新,每一次修改,整个事情变得越来越好。
There is still no end to the changes in sight but it feels a lot more stable now than a few months ago, and some API design patterns begin to emerge. I felt like now is a good time to explore this a bit more and started to rewrite my redis library to fit better into the scope of the language.
外观变化还没有结束,但是现在感觉比几个月前要稳定得多,并且一些API设计模式开始出现。 我觉得现在是探索此内容的好时机,并开始重写我的redis库 ,使其更适合该语言的范围。
The three languages where I do most of my work in are Python, C and C++. To C++ I have a very ambivalent relationship because I never quite know which part of the language I should use. C is straightforward because the language is tiny. C++ on the other hand has all of those features where you need to pick yourself a subset that you can use and it’s almost guaranteed that someone else picks a different one. The worst part there is that you can get into really holy wars about what you should be doing. I have a huge hatred towards both the STL and boost and that has existed even before I worked in the games industry. However every time the topic comes up there is at least someone who tells me I’m wrong and don’t understand the language.
我从事大部分工作的三种语言是Python,C和C ++。 对于C ++,我有一个非常矛盾的关系,因为我不太清楚应该使用哪种语言。 C很简单,因为语言很小。 另一方面,C ++具有所有这些功能,您需要自己选择一个可以使用的子集,并且几乎可以保证有人选择了另一个子集。 最糟糕的是,您可能会陷入一场真正的圣战,即应该做什么。 我对STL和Boost都怀有极大的仇恨,甚至在我从事游戏行业之前就已经存在。 但是,每次出现该主题时,至少都会有人告诉我我错了并且不理解该语言。
Rust for me fits where I use Python, C and C++ but it fills that spot in very different categories. Python I use as language for writing small tools as well as large scale server software. Python there works well for me primarily because the ecosystem is large and when it breaks it’s a super straightforward thing to debug. It also keeps running and can report if things go wrong.
Rust对我来说很适合我使用Python,C和C ++的地方,但它在非常不同的类别中占据了那个位置。 我将Python用作编写小型工具和大型服务器软件的语言。 那里的Python对我来说效果很好,主要是因为生态系统很大,当它崩溃时,调试起来非常简单。 它还可以继续运行,并可以报告是否出现问题。
However one interesting thing about Python is that unlike many other dynamic languages, Python feels very predictable. For me this is largely because I am a lot less dependent on the garbage collector than in many other languages. The reason for this is that Python for me means CPython and CPython means refcounting. I’m the guy who will go through your Python codebase and break up cycles by introducing weak references. Who will put a refcount check before and after requests to make sure we’re not building up cycles. Why? Because I like when you can reason about what the system is doing. I’m not crazy and will disable the cycle collector but I want it to be predictable.
然而,关于Python的一件有趣的事情是,与许多其他动态语言不同,Python感觉非常可预测。 对我来说,这主要是因为与许多其他语言相比,我对垃圾收集器的依赖性大大降低。 这是因为Python对我来说意味着CPython,而CPython意味着refcounting。 我是将通过您的Python代码库并通过引入弱引用来破坏周期的家伙。 谁将在请求前后进行refcount检查,以确保我们没有建立周期。 为什么? 因为我喜欢您什么时候可以推断系统的运行状况。 我并不疯狂,将禁用循环收集器,但我希望它是可预测的。
Sure, Python is slow, Python has really bad concurrency support, the interpreter is quite weak and it really feels like it should work differently, but it does not cause me big problems. I can start it and it will still be there and running when I come back a month afterwards.
当然,Python速度很慢,Python的并发支持确实很差,解释器很弱,确实感觉应该以不同的方式工作,但这并不会给我带来大麻烦。 我可以启动它,并且一个月后回来时它仍将在那里运行。
Rust is in your face with memory and data. It’s very much like C and C++. However unlike C and C++ it feels more like you’re programming with Python from the API point of view because of the type inference and because the API of the standard library was clearly written with programmer satisfaction in mind.
Rust面对您的内存和数据。 这非常类似于C和C ++。 但是,与C和C ++不同,由于类型推断以及标准库的API显然是在程序员满意的基础上编写的,因此从API的角度看,您更像是使用Python进行编程。
I think an interesting thing about programming in Rust is that in the beginning you will run into walls. It’s clearly not Python so lots of things you can get away with in Python do not work in Rust. At the same time it’s not C++ and the borrow checker will become your greatest enemy. You will write some code and say: this stuff really should work, why do you think you know better and stop me from doing this you stupid thing?
我认为关于Rust编程的一件有趣的事情是,一开始您会碰壁。 显然,这不是Python,因此在Python中可以摆脱的许多事情在Rust中不起作用。 同时,它不是C ++,借阅检查器将成为您最大的敌人。 您将编写一些代码并说:这东西确实应该起作用,为什么您认为您更了解并阻止我这样做,这是您的愚蠢的事情?
The truth is that the borrow checker is not perfect. The borrow checker prevents you from doing dangerous things and it does that. However it often feels too restrictive. In my experience though the borrow checker actually is wrong much less often than you think it is and just requires you to think a bit differently.
事实是借位检查器并不完美。 借阅检查器可防止您执行危险的操作,并且这样做。 但是,它常常感到过于严格。 根据我的经验,借位检查器实际上是错误的原因,它的发生频率要比您认为的要少得多,而只是要求您有所不同。
I like the borrow checker personally a lot. I agree with that sometimes you feel there should be a way to disable it, but when you think more about it you are quite happy it’s there. The borrow checker prevents you from building up some of the worst technical debt you can acquire, the kind of debt which you can never repay. The Python programming language acquired a global interpreter lock when it got threading support and it really required it ever since the language existed. The interpreter was written in a way that nowadays we are not sure how to make it concurrent.
我个人非常喜欢借阅检查器。 我同意有时您认为应该有一种禁用它的方法,但是当您对它进行更多考虑时,您会很高兴看到它存在。 借贷检查器可防止您积累一些可以获取的最差的技术债务,这种债务永远无法偿还。 Python编程语言在获得线程支持时获得了全局解释器锁,并且自从该语言存在以来就一直需要它。 解释器的编写方式目前尚不确定如何使其并发。
If you try, you can still make terrible decisions for concurrency in Rust, but you really need to go out of your way to do it. The language forces you to think more and I believe that’s a good thing. I don’t want to become another “objective oriented programming is the billion dollar mistake” preacher but I do think that the language decides largely what code people write. Because subclassing is easy in C++, Java, Python and many more languages this is what we write. And then birds are instances of animal classes. If you take that tool away you start thinking differently and that’s a good thing. CPUs stop getting faster and looking at one object at the time really no longer makes any sense at all. We need to start reasoning a lot more about collections of things and what transformations we actually want to do.
如果尝试,仍然可以在Rust中为并发做出糟糕的决定,但是您确实需要竭尽全力。 语言迫使您多思考,我相信这是一件好事。 我不想成为另一个“面向对象的程序设计是十亿美元的错误”的传教士,但我确实认为该语言在很大程度上决定着人们编写的代码。 因为在C ++,Java,Python和许多其他语言中子类化很容易,所以这就是我们编写的内容。 然后鸟类就是动物类的实例。 如果您不使用该工具,那么您会开始以不同的方式思考,这是一件好事。 CPU不再变得越来越快,并且一次查看一个对象确实不再有意义。 我们需要开始更多地思考事物的集合以及我们实际想要进行的转换。
For me programming in Rust is pure joy. Yes I still don’t agree with everything the language currently forces me to do but I can’t say I have enjoyed programming that much in a long time. It gives me new ideas how to solve problems and I can’t wait for the language to get stable.
对我来说,在Rust中编程是纯粹的快乐。 是的,我仍然不同意该语言当前强迫我做的所有事情,但是我不能说我很长时间以来一直喜欢编程。 它为我提供了解决问题的新思路,我迫不及待地希望语言变得稳定。
Rust is inspiring for many reasons. The biggest reason I like it is because it’s practical. I tried Haskell, I tried Erlang and neither of those languages spoke “I am a practical language” to me. I know there are many programmers that adore them, but they are not for me. Even if I could love those languages, other programmers would never do and that takes a lot of enjoyment away.
Rust受到鼓舞的原因有很多。 我喜欢它的最大原因是因为它很实用。 我尝试了Haskell,我尝试了Erlang,但这些语言都没有对我说“我是一门实用语言”。 我知道有很多程序员喜欢他们,但是他们不适合我。 即使我喜欢那些语言,其他程序员也不会这样做,这带走了很多乐趣。
Rust is something that anyone can pick up and it’s fun all around. First of all (unless you hit a compiler bug) it won’t crash on you. It also gives you nice error messages if it fails compiling. Not perfect, but pretty good. It comes with a package manager that handles dependencies for you, so you can start using libraries other people wrote without having to deal with a crappy or non existing ecosystem. Python got much better over the years but packaging is still the biggest frustration people have. Cargo (rust’s package manager) is barely half a year old, but it has a full time developer on it and it’s fun to use.
Rust是任何人都可以拿到的东西,它的乐趣无处不在。 首先(除非您遇到了编译器错误),它不会对您造成崩溃。 如果编译失败,它还会为您提供不错的错误消息。 不完美,但还不错。 它带有一个程序包管理器,可以为您处理依赖项,因此您可以开始使用其他人编写的库,而不必处理糟糕或不存在的生态系统。 这些年来,Python变得越来越好,但是包装仍然是人们最大的挫败感。 Cargo (Rust的包装经理)才刚刚成立半年,但是它有专职开发人员,使用起来很有趣。
Even just the installation experience of the language is top notch. It gives you compiler + documentation tool + package manager and you’re good to go. The fact alone that it has a documentation tool that spits out beautifully looking documentation out of the box is a major contributor to enjoying programming in it. While I wish it had a bit more of Sphinx and a bit less of javadoc, it’s a really good start for more.
即使只是该语言的安装经验也是一流的。 它为您提供了编译器+文档工具+程序包管理器,您一切顺利。 仅它具有一个文档工具就可以开箱即用地发出漂亮的文档这一事实,就成为在其中享受编程的主要贡献者。 虽然我希望它有更多的Sphinx和更少的javadoc,但这确实是一个不错的开始。
But what’s really inspiring about Rust are the small things. When I first played with Rust what amazed me the most was the good FFI support. Not only could you call into C libraries easily: it also found and linked them for you. There is so much more though and it’s hidden everywhere. There is a macro (include_str!) that will read a file next to your source at compile time into a string into your binary (How cool is that!?). Not only can you bake in contents of files, you can also pull environment variables into your binaries for instance.
include_str!
The most interesting part currently is definitely finding out how to properly write APIs for Rust. Rust as a language is undoubtedly more complex than most other but thankfully not in a way that it overwhelms you. What makes Rust complex from an API point of view is that as a programmer you feel a bit of a tension between writing the straightforward code that you expect when programming in a systems language and providing a nice high level API like you expect in Python.
当前最有趣的部分肯定是找到如何正确编写Rust的API的方法。 毫无疑问,Rust作为一种语言比大多数其他语言都更加复杂,但值得庆幸的是,Rust并没有让您不知所措。 从API的角度来看,使Rust变得复杂的是,作为一名程序员,您在编写使用系统语言进行编程时期望的简单代码与提供Python中期望的高级API之间会感到有些紧张。
The reason I feel making nice APIs is because the language encourages it. First of all the language in itself is super expressive and it makes a lot of fun to write things in it — on the other hand there is just so much possibility.
我之所以做出不错的API,是因为该语言鼓励这样做。 首先,语言本身具有超强的表现力,用它编写东西很有趣–另一方面,可能性也非常大。
To give you an idea why it’s fun to design APIs for Rust is that the type system is just so damn good. So Rust is statically type checked but it has inference so you get away with writing really beautiful code. In my rust driver for instance, you can write code like this:
让您知道为什么为Rust设计API有趣的原因是类型系统是如此之好。 因此,Rust进行了静态类型检查,但它具有推理功能,因此您无需编写真正漂亮的代码。 例如,在我的rust驱动程序中,您可以编写如下代码:
externextern cratecrate redisredis ;;
fnfn mainmain ()() {{
letlet clientclient == redisredis :::: ClientClient :::: openopen (( "redis://127.0.0.1/""redis://127.0.0.1/" ).). unwrapunwrap ();();
letlet concon == clientclient .. get_connectionget_connection ().(). unwrapunwrap ();();
letlet (( k1k1 ,, k2k2 )) :: (( i32i32 ,, i32i32 )) == redisredis :::: pipepipe ()()
.. cmdcmd (( "SET""SET" ).). argarg (( "key_1""key_1" ).). argarg (( 4242 ii ).). ignoreignore ()()
.. cmdcmd (( "SET""SET" ).). argarg (( "key_2""key_2" ).). argarg (( 4343 ii ).). ignoreignore ()()
.. cmdcmd (( "GET""GET" ).). argarg (( "key_1""key_1" ))
.. cmdcmd (( "GET""GET" ).). argarg (( "key_2""key_2" ).). queryquery (( && concon ).). unwrapunwrap ();();
printlnprintln !! (( "result = {}""result = {}" ,, k1k1 ++ k2k2 ););
}}
To give you an idea how the same code looks in Python currently:
为了让您了解当前相同的代码在Python中的外观:
What I find interesting about this is that the Rust library is nearly as small and clear as the Python one, but is a much lower-level binding. Unlike the Python library which gives each call a separate method, the Rust library (because quite new) only wraps the low-level API and you need to create the request manually by chaining calls for each argument. Yet the end result for a user is nearly as nice. Granted there is extra handling needed in Rust for the errors (which I avoided here a bit by using unwrap
unwrap避免了一些错误,这会使应用程序终止,但是在Python版本中,我也错过了错误处理的情况)。
The cool thing though is that the Rust library is completely type safe. And yet in total there are exactly two places where types are mentioned and that’s the same ones, where a cast to an integer was necessary in Python.
不过,很酷的事情是Rust库是完全类型安全的。 然而,总共总共有两个地方提到了类型,这是相同的,在Python中必须将其强制转换为整数。
This however is not the best we could do in Rust. Rust has compiler extensions which open up a whole range of possibilities. For instance there is a Rust library which statically verifies that Postgres SQL commands are well formed: rust-postgres-macros:
但是,这并不是我们在Rust中能做的最好的事情。 Rust具有编译器扩展,这扩展了各种可能性。 例如,有一个Rust库可以静态验证Postgres SQL命令的格式是否正确: rust-postgres-macros :
test.rs:8:26: 8:63 error: Invalid syntax at position 10: syntax error at or near "FORM"
test.rs:8 let bad_query = sql!("SELECT * FORM users WEHRE name = $1");
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
This sort of stuff excites me a whole lot.
这种东西使我非常兴奋。
(If you’re into API design in rust, join us in #rust-apidesign on the)
(如果您对rust的API设计感兴趣,请加入上的#rust-apidesign)
Is Rust’s memory tracking concept strong enough that we will accept it as a valid programming model? I am not sure. I do believe though that Rust can stand on its own feet already. Even if it would turn out that the borrow checker is not sound, I believe it would not hurt the language at all to widespread adoption. It’s shaping up to be a really good language, it works really well without GC and you can use it without a runtime.
Rust的内存跟踪概念是否足够强大,足以被我们接受为有效的编程模型? 我不确定。 我确实相信Rust可以自己站起来。 即使事实证明借阅检查器不健全,我相信它也不会对广泛采用语言造成损害。 它被塑造成一种非常好的语言,没有GC就能很好地工作,而且您无需运行时就可以使用它。
Rust is an exceptionally good open source project. And it needs more helping hands. The Windows support (while getting better) especially needs more love.
Rust是一个非常好的开源项目。 它需要更多的帮助。 Windows支持(虽然越来越好)尤其需要更多的爱。
If there is interest in some more practical Rust experience I will probably write something up about my experience making redis-rs.
如果对一些更实用的Rust经验感兴趣,我可能会写一些关于我的redis-rs经验。
翻译自: https://www.pybloggers.com/2014/10/a-fresh-look-at-rust/
rust一键开服