Contents

Scala AND Rust ?

Science based programming language learning

Exploring two prorgamming languages at the same time seems like a bad idea, doesn’t it? Well, good learning sources say that the best approaches to effective, expert learnings are:

  • interleaving (study both languages concurrently — never sequentially)
  • spaced repetition
  • deliberate practice at the edge of your comfort zone
  • retrieval practice (close the docs)
  • and the explanation effect

Looks like a very good idea to me.

  • What if I could somehow optimize the learning process by utilizing all of them?
  • What if I create a protocol with stuff I want to learn split into some stages and implement those science-based approaches to both “ingest” and “practice”?

Risks:

  • mental overwhelm - burnout, self-blame and all the internal struggle
  • ADD effect of not sticking to the protocol

So I engaged AI for preparing

  • the plan for learning Scala and Rust in parallel
  • the Anki deck for learning
  • and the SPA with the deck as well

The plan

This is the suplementary plan to the one I generated specifically for Scala in Scala Learning Path, however:

  • original plan covers Scala only and is defined for 8 weeks
  • this plan covers both languages, Scala AND Rust

Resources

I think I’m ready!

The execution

As an exercise, after I wrote simple anniversary lister in Rust (see diary) I created the same program in Scala (see scala-diary).

What I have learned

  1. Once you are slighly more familiar with one language your tendency is to use it for all auxiliatory tools. For example, to generate a list of events as input for both Rust and Scala versions I reach out to Rust (see Diary Generator)
  2. Writing unit tests inside a source file and not in a different directory helps to practice DDD, eliminating the need to switch between production and test code
  3. Modelling errors: I used enums - in Rust I’m aware of crates like thiserror and anyhow (two top-level on rates.io in error-handling category)
  4. Enums: in Scala I used Scala Enums as Left of the Either; in Rust I also modelled them using Rust Enums in Result type.
  • those are different. Scala enums:
    • are “used to define a type consisting of a set of named values” (in scala 2 they were represented as objects)
    • are based on reflection
    • can be used as Java Enums by extending java.lang.Enum:
    1
    2
    
    enum ScalaEnum extebds Enum[ScalaEnum]:
      case ValueOne, ValueTwo
    
    • can have fields - and different shapes:
    1
    2
    3
    
      enum EBase(val n:Int):
        case EOne(val s: String) extends EBase(1)
        case ETwo(val d: java.time.Duration) extends EBase(2)
    
    but then you cannot use EnumClass.fromOrdinal (obviously) which works only for non-parameterized enums (which still can have some constant data):
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    
    enum Foo(val n: Int):
       case Bar extends Foo(7)
       case  Baz extends Foo(345)
    
    scala> Foo.Bar.n
    val res12: Int = 7
    
    scala> Foo.fromOrdinal(0)
    val res15: Foo = Bar
    
    scala> Foo.fromOrdinal(0).n
    val res16: Int = 7
    
    • previously (in scala 2.x) the pattern matching with exhaustiveness checking and with possibility to use different data required reaching out for sealed class hierarchies:
    1
    2
    3
    
       sealed class Base(val n: Int)
       case class One(s: String, t: java.time.LocalDate) extends Base(1)
       case class Two(d: java.time.Duration) extends Base(2)
    
    To be honest, the only valuable thing enums bring to the table - afaik - is its Enum’s api (.values, fromOrdinal(Int)) - as stated in this explanation - enums is just a syntactic sugar 🍭 for sealed classes.
  • Rust Enums are quite similar I think:
    • variants can have different shapes
    • behave more like a sealed class hierarchy in Scala
    • can be pattern-matched as well
  1. It is so cool to have scala REPL especially at the beginning of the journey. Rust does not have that.

Resources

For reading

For experimenting

Happy Hacking!