Learning Rust :safe:

I’m learning a bit of Rust and starting this topic in case anyone is interested in discussing Rust or wants to help each other out with learning the language.

No need to be a programmer, anything to do with Rust is relevant to this topic including n00b questions. We can always create extra topics if needed, or maybe start a developer focused topic in the Dev forum.

What is Rust?

Rust is the programming language used to create the core of the SAFE Network, and was chosen because it is designed for building secure, reliable code in ways that avoid bugs and make difficult things easier to do well.

Official Rust Resources

Using Rust

  • Rust-Lang (website) is a good place to start. Lots of good resources and answers to your early questions can be found here, along with links to lessons, examples, tools etc.
  • The Rust Programming Language (book).
    An excellent tutorial style walk through all the features of Rust, free online and available in print.
  • Asynchronous Programming in Rust (book). How to use Rust’s asynchronous programming features (async, await etc.)
  • The Rust Standard Library (web reference)

Contributing to Rust

  • Rust Forge (website) provides supplementary documentation for members and contributors of the Rust programming language
  • Rustc Dev Guide (website) provides documentation on how to contribute to the Rust compiler

Other Resources

Error Handling

Error handling is important to grasp early on and doesn’t get good coverage in the official resources, so I encourage everyone to not skip this or leave it too long before you get to grips with it. Otherwise you’ll likely end up writing dodgy code without even realising, because almost all the example snippets lead you towards that anti-pattern. David has given some tips and encouragement in Rust error handling for production and test and a few good getting started resources are listed below.

Contemporary recommendations seem consistently to be the anyhow crate for apps and thiserror for libraries rather than error-chain (old) or failure (deprecated). Here are some 2019-2020 sources:

  • Rust: Structuring and handling errors in 2020 (blog)
    This is an excellent tutorial on the state of the art and a great overview of anyhow and thiserror. Also see the linked Reddit discussion about hand coding in order to avoid needing anyhow and the compilation time savings that can bring.
  • Idiomatic error handling and error chaining (July 2020, Rust forum)
  • Error handling survey (November 2019, blog)
  • Error Handling (Rust book)

Async, Futures and Concurrency

  • Async programming in Rust with async-std (book). An alternative book on asynchronous programming in Rust.
  • Rust concurrency patterns: regretless concurrency? (blog post) by Gregory Terzian, a core team member of the Servo browser engine. A blog post about higher level concurrency concepts in Rust.

Tools

  • VSCode/VSCodium works well as an integrated development environment with syntax highlighting, language and library reference using ‘intellisense’, debugger, clippy linter, and code formatter. See using VS Code with Rust.

Share What You Are Doing With Rust

I’d love to hear from anyone about their adventures with Rust! Please post as little or as much as you like on this topic about what you are up to, or are thinking about doing:

  • might be an idea for an app you are figuring out
  • maybe you’re reading something
  • maybe you are trying to understand something that just doesn’t make sense yet
  • maybe you are working on a Maidsafe issue
  • or building a simple game for the hell of it
  • maybe there’s a particular area that you are interested in, such as concurrent or parallel compassion
  • or tell use what you’ve done recently, hope to do soon etc
  • anything really

What are you up to, or thinking about with Rust?


This topic is a Wiki so anyone can add resources to this post.

26 Likes

To kick things off here’s what I’m up to. I’m interested in learning Rust because I think it will be a useful skill, particularly around the SAFE Network. This has begun now because I came across syncer, which is written in Rust and I’m trying to modify it to work with SAFE. I managed to do that, but found I can’t get it fully working without some extra features being added to the SAFE CLI, and am learning more to see if I might have a go at them.

To learn Rust, I’m reading The Rust Programming Language (see the OP), which is an excellent book and a very good way to get up to speed fast if you already know a systems language like C++. I’m a little over half way in a few days, have made quite a few notes, and getting to the hard bits :smile:

Before that I had a look at the SAFE CLI code and realised I would need to understand Rust much better before thinking about modifying it.

I’ve got a development environment set up in VS Codium (the privacy version of VS Code), which makes working with Rust very nice, and saves a lot of the work in catching errors, formatting code and helping me find what I need all in one place. And it has a built in source code debugger. :sunglasses: (Here’s a topic on using VS Code with Rust)

8 Likes

abc excercises https://exercism.io/tracks/rust/exercises/
and
lists of what crates there are https://crates.io/

6 Likes

Rust by Example
https://doc.rust-lang.org/rust-by-example/

24 days of Rust
https://zsiciarz.github.io/24daysofrust/index.html

Building a Roguelike game
http://bfnightly.bracketproductions.com/rustbook/chapter_0.html

6 Likes

Good work @happybeing

I have not much to add other than to say I read that rust had entered a top 20 popularity ranking, so it seems its been a good choice by David and the team: https://www.zdnet.com/article/programming-languages-rust-enters-top-20-popularity-rankings-for-the-first-time/

Apparently 86% of devs on stack overflow said they are keen to use it but only 5% are currently using it. Opportunity? https://insights.stackoverflow.com/survey/2020#overview

6 Likes

Thanks @happybeing for starting this wiki. I started reading Rust’s “the book” this weekend. I love the memory safety but ownership and borrowing require a bit of getting used to for me.
I initially had path and linking errors while setting up both the VS Code and Eclipse Corrosion IDEs on Mac Catalina. If you encounter that, try updating the Xcode app before digging into anything else. While I am not using the Xcode app at all, the update resolved the system configurations issues for the other IDEs.
Good luck all.

Eclipse Corrosion - provides development tools for Rust and Cargo inside the Eclipse IDE. https://projects.eclipse.org/projects/tools.corrosion

6 Likes

Great that you are reading this book too, I’ll be interested to hear more and chat about things like ownership.

I think ownership seems weird at first, because Rust is making it visible and has added checks to stop you screwing it up. With other systems languages your code would just crash! [Ed: If you’re lucky! h/t David]

Rust stops you screwing it up, helps you understand why it won’t compile, and requires you to think about what you want to happen so you can tell the compiler explicitly.

I think it starts to look very complicated because the power of Rust is in how concisely you can describe and how precisely you get to control several things like ownership, generic types, and closures. I was finding this tricky yesterday, but it helped a lot to look at each code example in those chapters and try to understand what the hieroglyphics mean. It will take practice, hard at first, but eventually will begin to gel I hope!

I expect to re-read those sections quite a few times, and have made notes with links so I can get back to where each feature is described quickly. I always find making notes helps me learn new stuff, even if I never look at them again which is usually the case.

I am both excited about and intimidated by the power of what I’ve just read about, and there’s quite a bit of the book still to go.

4 Likes

It’s actually better, other languages might not crash and do worse (crash is really best case). Many langs like c/c++ will let you read an apparently cleared/freed mem location and it might work. It’s based on the fact the system may not have allocated that mem yet so it knida still works. Even worse the allocator does use the mem and something unexpected is in there. It might still “work” but now your system is in undefined behavior state.

So I see rust mem handling and also immutable by default values as saying to you the dev, “I understand what you are doing, I have confirmed there are no memory safety issues and you won’t be suprised”.

6 Likes

List of some less known crates I’ve found useful:

  • nohash-hasher – useful for hash maps of simple primitives
  • qcell – alternative for RefCell, which is compiled time checked.
  • ring – hmac, digest with good performance
  • snmalloc-rs – faster allocations
  • coarsetime – fast time access
4 Likes

Share What You Are Doing With Rust

I’d love to hear from anyone about their adventures with Rust! Please post as little or as much as you like on this topic about what you are up to, or are thinking about doing:

  • might be an idea for an app you are figuring out
  • maybe you’re reading something
  • maybe you are trying to understand something that just doesn’t make sense yet
  • maybe you are working on a Maidsafe issue
  • or building a simple game for the hell of it
  • maybe there’s a particular area that you are interested in, such as concurrent or parallel compassion
  • or tell use what you’ve done recently, hope to do soon etc
  • anything really

What are you up to, or thinking about with Rust?

8 Likes

Me and a few others are building a kinda network thing :slight_smile:

24 Likes

(Prematurely) announcing vdash

In the hope that one day I can write some Rust for SAFE I’ve started a small project using Rust to create a terminal based dashboard to monitor one or more local SAFE vaults.

The code runs but doesn’t do anything useful yet. The plan is to monitor the logfile of each vault on the local machine and to provide summary and detail views within a terminal based dashboard.

If anyone fancies learning Rust by working with me on this, fork it and send me a pull request. There’s a list of to-do’s in the README or use your imagination.

I plan to spin off a fork which people can use to make other logfile based dashboards, and then focus on the SAFE specific version in this REPO:

10 Likes

Quick Rust question… how do I define a shorter term for the type of terminal so I don’t have this ridiculously long line in every function definition:

fn draw_dash_summary(
  terminal: &mut tui::terminal::Terminal<TermionBackend<termion::screen::AlternateScreen<termion::input::MouseTerminal<termion::raw::RawTerminal<std::io::Stdout>>>>>, 
  dash_state: &DashState, 
  monitors: &HashMap<String, 
  LogMonitor>)
  -> std::io::Result<()> {
2 Likes

type MyTerm = <that great big long thing> should do it I think.

5 Likes

Another hopefully quick Rust question:

  • what are MaidSafe using for async/futures and what is the plan longer term?

Background: my impression was that MaidSafe have been using the tokio implementation of futures, and that this was being switched to native implementations now these were made part of the v1.39 (I think) rustc release. I think I lack understanding here!

On that basis I was wondering about the implications of safe-dash relying on a library (linemux) which uses the tokio futures, pros and cons, and whether at some point it might make sense change linemux to drop the tokio async.

I think some of the above is wrong based on the (very helpful) response I got from the linemux author (here). I think he says the futures runtime isn’t part of the standard compiler, which means I’m confused about what MaidSafe are doing, what the options are and how to select libraries which utilise async code. I obviously lack understanding of how futures relate to async, and so on.

2 Likes

It’s not super clear in async. Basics are
There are decorators for functions aync is the best example. The std library implements that and await. The return of async things are Futures and IIRC based on futures version 3.0 and up.

So the core lib has the async / await syntax, futures provide the return types.

Then you have the runtime and that is where the event loop/waiting magic happens. You will see stuff like '[tokio-main] or #[async_main] and so on. These provide the runtime (event loop etc.) for you.

So libs, use async/await and don’t care which runtime you end up with. OK now the caveats, std-async and tokio-async have some slight differences in the future return type for some calls. There is some compatibility issues but they are being worked on. As a base lib (quinn) we use makes use of tokio we just did as well. However it’s not a huge difference and libs generally (unless they use specific to tokio calls) shoudl not worry, in theory. To be safe use tokio if your deps also do, but you may not need to and in future the choice will be much easier as all runtimes should be compatible.

Hope that mess makes sense?

4 Likes

It all helps, thanks David. I’m starting to get the picture.

2 Likes

Hi @happybeing,
I’ve added the following entry to the wiki:

  • Rust concurrency patterns: regretless concurrency? (blog post) by Gregory Terzian, a core team member of the Servo browser engine. A blog post about higher level concurrency concepts in Rust.

Gregory Terzian writes some excellent blog posts on Medium about concurrency concepts in Rust. It is not so much about async/await, but rather “concurrency-by-message-passing-via-channels”. It’s super interesting and well written and probably most suitable for Rustaceans (Rust developers call themselves “Rustaceans”), who see themselves between beginner and intermediate level.

My Rust journey

I’ve started programming in Rust in January 2019 in my spare time. In order for me to have a long term project and a challenge, where I can really dive into Rust, I’ve decided to build a transpiler (basically a compiler that transforms source code into other source code) for a programming language (called X++) that is used in the ERP system Dynamics 365 by Microsoft (my daily work is related to this ERP system - I don’t work for Microsoft). It can basically transform the following SQL-like declarative form into an analogue procedural form:

// declarative SQL-like form
select custTable
  where custTable.AccountNum == '40002'
  join address
    where address.Id == custTable.AddressId
// ----------------------------------------------------------------------
// the transpiler transforms it roughly into the following procedural form:
qbdsCustTable = query.addDataSource(custTable.TableId);
qbdsCustTable.addRange(fieldNum(CustTable, AccountNum)).value('4002');
qbdsAddress = qbdsCustTable.addDataSource(address.TableId);
qbdsAddress.addLink(fieldNum(CustTable, AddressId), fieldNum(Address, Id));

Keep in mind that both forms are valid X++, but in some situations you need the declarative form and in other situations you need the (arguably more complex) procedural form. The transpiler allows you to generate this procedural form from the SQL-like form.
Things that are still missing:

  • output as string, but work has started and I’m getting there
  • provide a browser frontend and use WebAssembly
  • probably a lot of nasty edge cases :smile:

I hope to open source this project soon, but if you know the SAFE network, you know that exact deadlines are hard. :smile: :kissing_heart:

Now is your turn :wink:

Thank you for opening this thread, @happybeing!
I can only encourage anyone, who is into programming, to learn Rust. It might be a little intimitating as a first language to learn, though. But if you are already programming in another language, please give Rust a try. It is beautiful!
And if we in this forum can’t convince you, then you might have a look at the annual StackOverflow survey, where Rust has been winning in the category Most Loved Technology/Programming language - for the years 2016, 2017, 2018, 2019, 2020 - that is 5 times in a row every year! :sparkles: :fireworks:

7 Likes

Here’s my first Rust thing:

logtail - a terminal dashboard for monitoring multiple logfiles (see crates.io)

If you have rustc installed you get install it using:

cargo install logtail

And try it with:

logtail /var/log/syslog /var/log/auth.log /var/log/lastlog /var/log/dmesg /var/log/kern.log /var/log/boot.log

You can switch the dashboard between vertical and horizontal layouts using ‘h’ or ‘v’.

I’ve only tested on Linux but think it may work as is on Mac if somebody would like to test that, please let me know. Same for Windows, although I think I might need to tweak the code for that - should not be hard if anyone wants to help me with that.

The code is available at logtail-dash and I’ll be continuing to develop a fork for monitoring SAFE vaults called vdash.


And great to have you here @janriemer! Thanks for adding to the resources in the OP.

4 Likes

Brilliant, one small issue, if you pass an invalid path then you will error here thread 'main' panicked at 'Unable to read line: Custom { kind: InvalidData, error: "stream did not contain valid UTF-8" }
. So good to catch those errors. However, very very cool Mark, really impressive indeed.

2 Likes