Idea for helping people(like me) further understand Rust!

Hey all, so perhaps there is something like this out there already, maybe not but either way, I had a thought that could help not only myself but perhaps others while learning Rust. I cant lie and say when I see this, im not slightly intimidated:

struct Point<T>{
  x:T,
  y:T
}

impl<T: Add<Output = T>> Add for Point<T> {
    type Output = Self;
    ...
}

Coming from higher level languages, this looks like Taglog to me(<<< natural born U.S. citizen). I know what this does, at least the most important aspect of, but I do not know how to verbalize this. I thought that it could be a great idea for the community to gather up some "cryptic" snippets of Rust code and perhaps put together a dictionary of sorts, or maybe even a cli app likes rustlings, on not only what it does but how to say this out loud. Could be a useful community project... Anyone???

2 Likes

Check out explaine.rs, it's still missing some stuff (it doesn't have an explanation for T: Add being a trait bound for example) and it doesn't seem like it's being actively worked on right now, but I think it should cover a decent amount of Rust's syntax.

3 Likes

That is a question that I have been pondering too.

My initial reaction to such examples is that it looks like "line noise". An impenetrable jumble of odd characters and symbols that may as well be random and meaningless. Much worse examples pass by here every day with the lifetime ticks all over the place.

Like you I have slowly come to be able to disentangle it and discern some meaning from it. It of course becomes easier with practice and familiarity.

So yes, how does a fluent Rustacean hear that with their inner voice? How would they read it out allowed? How would they write it out as English prose such that anyone unfamiliar with Rust would understand it?

A long while ago somebody wrote a translator from C to English that did exactly that. (It could also translate such English description back to C). How would that go for Rust?

1 Like

A point consists of two values of the same type. If the type inside the point can be added with itself, then so can the point type.

5 Likes

Not saying this is "the right way" or something, but I think I would read it aloud as:

struct Point with type T: x isT, y isT.

impl (with type T that is Add with Output equals T) Add for Point of T: type Output equals Self. ...

4 Likes

so maybe a more formal expl.:

and more 'native':

Focusing on this part instead (i.e. not addressing the struct definition), my first instinct would be to move the bound into a where clause in order to make the line shorter. So we get

impl<T> Add for Point<T>
where
    T: Add<Output = T>
{
    type Output = Self;
    /// ···
}

I guess my direct verbalization would then sound like

impl T Add for Point T, where T implements Add, Output equals T
type Output equals Self

It’s analogous to verbalization of mathematical formalisms in that the verbalization doesn’t necessarily unambiguously describe what’s written down any more. Also I don’t always verbalize everything and in particular I don’t read everything linearly. For example for your original code, I guess I’d still do a similar order to the linear order of my adaptation with where. So, reading the Add for Point<T> part first, then the T: Add<Output = T> condition that was written to the left of it, and finally the Output = Self definition below.

If I was to describe this trait implementation unambiguously to someone else verbally, maybe something like the following works:

Implement Add generically for Point<T>, for types T that implement Add themselves with associated type Output = T.

In this implementation, define the associated type Output to be Self.

Just noticing afterwards, I haven’t even fully eliminated formal notation. Point<T> still sounds like “point t” and Output = T is “output equals t”; even though, actually, in my head I always hear the German word for “=” which is “gleich” ( :loud_sound: ) which is so much nicer because it’s only a single syllable, but that’s perhaps going a bit off-topic.

3 Likes

Sorry for the late response. I havent really had much of a chance to swing on the forums for a minute as Ive been having some trouble getting and keeping my computer stuff charged. But anyways. Reading over what you've written down,... wow. Haha, talk about a spinning head, totally threw me for a loop.

What I was trying to accomplish I guess was figuring out a way to not only formally verbalize these kinds of things but also a way to verbalize it in "layman's terms" so to speak. For example someone learning spanish in high school learns to speak spanish in a very formal way whereas if you go spend some time in a mexican neighborhood, you will quickly realize that thats not how most of them speak. The difference for example is greeting someone formally like "Hola, como estas?" versus whats considered slang but has pretty much the same meaning "Que onda?", plus its easier to say. Learning how to speak formally is important, yes, but learning how to speak in a more relaxed fashion grants the benefit of being able to express yourself more effectively and knowing both grants greater understanding. The reason why I think being able to verbalize these things is important is because once you know the name of something previously unknown, you are given the power to talk about, learn about and even teach the subject of, in a much more clear and concise way. Hopefully that makes sense.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.