r/linux • • Apr 14 '21

Kernel [RFC] Rust support in the Linux kernel

https://lkml.org/lkml/2021/4/14/1023
608 Upvotes

316 comments sorted by

66

u/zmxyzmz Apr 14 '21 edited Apr 14 '21

The RFC for Rust support in the Linux kernel has been released.

See the project Github page here.

7

u/GraionDilach Apr 15 '21

I don't like this because this cuts from the platforms.

When the Python's cryptosecurity devs added Rust weeks ago, it turned out that Rust/LLVM does not support all the platforms GCC does and one of the platforms falling out was AIX because "AIX is dead based on our download numbers" (most of the sysadmins on AIX do not rely on thirdparty patch downloads, but IBM's FixCentral as a central repo, so download counts are meaningless) although this was already decided by the time I found the discussion.

Now python-crypto is in userland, so it can call shots like that and drop platforms left-and-right but if Rust gets into the Linux kernel, then there will be lost platforms (Linux-on-Power could be one) and there should be a decision made early if losing platforms due to LLVM having fewer architectures supported is affordable.

23

u/zmxyzmz Apr 15 '21

This is a known issue in which there is active progress. For example there are projects writing a GCC backend for the rust compiler (rustc_codegen_gcc) and to write a GCC front end for rust (gcc-rs). While these are still works in progress, the problems you mention are definitely being addressed. This is partially why the focus is on rust for drivers only at the moment.

13

u/ElvishJerricco Apr 15 '21

They've stated in previous threads that rust modules will only be added to the kernel if they're meant for hardware that is exclusively supported by platforms that rust supports. i.e. no modules will be written in rust if they could be usable on platforms that rust doesn't support.

9

u/ffscc Apr 15 '21 edited Apr 15 '21

... and one of the platforms falling out was AIX ...

FYI, It was announced last year that they are transitioning IBM XL C/C++ to Clang/LLVM. So support should be coming soon.

2

u/Booty_Bumping Apr 19 '21

For this reason, Rust may be best to first only use for x86-64 and ARM related drivers. These problems are solveable, they're not going to go nuclear on compatibility all at once like pycryptography did.

0

u/[deleted] Apr 15 '21

[deleted]

3

u/Shikadi297 Apr 16 '21

Mozilla invented Rust so makes sense that they would use it...

What platforms were you trying to run Firefox on that lost support? Just curious

157

u/KingStannis2020 Apr 14 '21

Linus' initial opinion: https://lkml.org/lkml/2021/4/14/1099

157

u/[deleted] Apr 14 '21

on the whole I don't hate it.

HOWEVER.

...

... my reaction comes from ignorance ...

The TL;DR. He has some valid seeming concerns, but get those addressed (by changes or by education) and it seems he might find it acceptable.

5

u/lestofante Apr 15 '21

alert: rust noob that has a little experience with rust embedded (no_std) and read all the post as yesterday.
yeah that panic! stuff should be removed, the problem the other guy pull up of calling some function from interrupt is already solved in rust embedded (something that CAN'T be fixed in C/C++!), and about what kind of mutex (linux style vs rust style) i would pretty much prefer the rust style as they are RAII and with generic you can wrap the resource in the mutex, so that you cant access it by accident, you must lock the resource, and the returned object lifetime will also control the unlock; it is a major advantage and since lifetime are used, most of it is managed in compile time.

0

u/[deleted] Apr 14 '21

[deleted]

118

u/jarfil Apr 15 '21 edited Jul 16 '23

CENSORED

2

u/Fmatosqg Apr 15 '21

That's right. Abstractions are never absolutely necessary, they're there to make things easier for somebody else.

Good point on the microkernel, though for the sake of these arguments I think all it would do (if Linux were microkernel) would be to move the discussion to some middle-ware that sits between the kernel and user land. Because that's where a lot of the drivers would sit.

-4

u/continous Apr 15 '21

Even if we take a more flexible approach, I think it is perfectly reasonable to say that Rust really has no proper place in the kernel.

8

u/[deleted] Apr 15 '21

That's just saying that you don't understand the security implications of C and anything about Rust.

→ More replies (1)

6

u/balsoft Apr 15 '21

I think Rust does have a place in kernel drivers. It is easier to write and maintain safe code in Rust than it is in C, and the performance hit is relatively small.

→ More replies (7)

0

u/[deleted] Apr 15 '21

[deleted]

49

u/WindowsHate Apr 15 '21

How exactly does writing drivers exclusively in C ameliorate this problem in ways that writing in Rust does not?

35

u/0x4A5753 Apr 15 '21

The exact vulnerabilities are above my head, I'm not a security researcher. However, I took some classes in college, have performed basic exploits, and my understanding is that the easiest weak link in an application with root access is the memory/stack. If you can find some way to make some code...say, a driver that reads I/O, read and execute some malicious code, the red team is probably gonna find a way around the bazillions of mitigations and defense mechanisms, to get root access, or at least some form of some elevated access.

Rust is relevant here, because that^ is what Rust is good at preventing. Writing memory-insecure code. By design it tries to enforce memory safety, whilst still preserving as much of the C mentality.

41

u/WindowsHate Apr 15 '21

The question was rhetorical; I was trying to get the guy above to answer it because he seems to think that Rust is somehow inherently more insecure than C (or alternatively, that adding Rust to the kernel would increase its attack surface compared to C), when in reality as you have correctly said, it's the other way around.

Thank you for the rundown though, I'm sure the comment will be useful to readers unfamiliar with the principles at play.

16

u/0x4A5753 Apr 15 '21

Ahh, whoosh. I missed that.

→ More replies (9)

5

u/Fmatosqg Apr 15 '21

If you get control of a driver you basically broke in.

And if an application has already root access you don't need to break in, it's like the door is open and you don't need to ask for the keys.

22

u/jarfil Apr 15 '21 edited May 12 '21

CENSORED

0

u/[deleted] Apr 15 '21

[deleted]

17

u/FredFS456 Apr 15 '21

Yes. All array accesses in (safe) Rust are bounds checked. Those that can be reasoned about at compile time are elided.

12

u/[deleted] Apr 15 '21

Considering that the kernel uses function pointers basically everywhere (it's kinda required when doing OO in C), prohibiting this would essentially mean "rewrite a huge chunk".

5

u/ffscc Apr 15 '21 edited Apr 15 '21

Function pointers might actually be the most unsafe thing in C.

It depends on what exactly you mean by "unsafe", but I'd consider unions far more dangerous than mere function pointers.

→ More replies (5)

5

u/TDplay Apr 15 '21

Some things could be made safer by disallowing certain practices like using function pointers in C.

When you disallow "unsafe" practices in C, you effectively ban the entire language. By design, C exposes the low-level hardware concepts. This makes it excellent for when you need low-level control (e.g. kernels, SIMD, embedded systems, etc), but makes it also very easy to shoot yourself in the foot if you're not careful.

Rust is a lot safer (the compiler enforces that you can't use any "unsafe" features), and the idea is that it doesn't lose the features of C since you can neatly tuck them away into unsafe blocks (and if your system implodes upon itself, chances are that you only need to look in the unsafe blocks to find the mistake). Of course, this all theoretical - I can't comment on how it would apply to a kernel, because I'm not a kernel developer.

9

u/Dont_Think_So Apr 15 '21

This is exactly why we need more Rust in the kernel. Memory safety guarantees make it harder to write vulnerable drivers.

→ More replies (1)

26

u/Shikadi297 Apr 15 '21

I think it's absolutely necessary to have memory safe code in the kernel to prevent security holes and bugs. Adding rust as an option can make this much easier (in theory), and once it's compiled you get assembly language just like you would with C. Saying rust shouldn't be there because it's not necessary is almost similar to saying we should turn off compiler warnings because they aren't necessary. It's not like they're trying to include a python interpreter

15

u/[deleted] Apr 15 '21

there are operating systems written in rust that still have memory management issues.

It's not like they're trying to include a python interpreter

no, but you now need rust and its whole host of dependencies for building. and if someone implements some kind of killer app kernel module for linux that is only done in rust, it's not going to be as portable as c code.

rust does not run everywhere, and it does not support as many plaftorms. i do hope this changes, and i like embracing new technology. just its overhead is a bit worrying.

8

u/Shikadi297 Apr 15 '21

Linux doesn't run on all the platforms C runs on, and it's probably less effort to port llvm to a new architecture than to port Linux to a new architecture. I'd wager the llvm supports all and more of the platforms Linux supports. You can see that with the apple m1, rust works on it just fine but Linux is barely bootable to a serial console right now.

The Linux kernel by itself has the cleanest, lightest build system dependencies I've seen in such a large project, it's beautiful, but adding the rust compiler to that doesn't make it less accessible since it's widely available. It's not like rustc is autotools, cargo files are simpler and more reliable than makefiles (though I'm not familiar with their cross compiling settings)

5

u/steveklabnik1 Apr 15 '21

Tiny note, the kernel won't be using Cargo; kbuild will invoke rustc directly.

3

u/Shikadi297 Apr 15 '21

Oh cool! Even better

5

u/Phoenix591 Apr 15 '21

For the near term its only for drivers, they arnt redoing the core.

2

u/[deleted] Apr 16 '21

exactly. imagine if some highly important driver arrives and it's implemented in rust.

4

u/Phoenix591 Apr 16 '21

They arnt going to rip out non rust drivers anytime soon, and I can't think of someone writing a driver in a language the platform the hardware is intended for can't use.

Also rust's platform support is slowly improving (check the rust release notes)

2

u/[deleted] Apr 15 '21 edited Apr 17 '21

Oh no, it won't run on m68k and other dead architectures!

3

u/[deleted] Apr 15 '21

i wasn't talking dead. i meant new architectures or someting exotic/niche enough that llvm doesn't care about it. also not sure about arm, risc or whatever runs on your router etc.

iinux is very portable, linux + rust - may not necessarily be.

4

u/[deleted] Apr 15 '21

Like what? LLVM arm and risc-v support is solid.

2

u/[deleted] Apr 16 '21

sure, nowadays it's mostly a case of arm or x86 with risc-v being on horizon. there is some sparc and ppc support, but not thoroughly tested. haven't seen any ibm power support, or i am not looking closely enough.

https://doc.rust-lang.org/nightly/rustc/platform-support.html

but for something that is not in that scope you are out of support. custom cpu like apple's m1, ibm power, avr (even if impractical), things may be difficult. not only do you have to adapt the linux kernel, but also the toolchain.

and there is also the case of having only one compiler for the codebase. only recently has linux mostly made itself compatible with clang, and now we're going full circle with rust.

i do not know how adaptable is gcc for new architectures, and i do not know how does llvm/rust compare here. maybe it is easier, maybe not. gcc simply has more coverage at the moment.

→ More replies (5)

0

u/[deleted] Apr 15 '21

[deleted]

3

u/Shikadi297 Apr 15 '21

I'd actually say rust is much more portable than Linux. Both can exist anywhere a C compiler does, but porting LLVM to a new architecture is much easier than porting Linux to a new architecture, for example the apple m1. It has an arm instruction set, so it's automatically targetable by the llvm and rust, but Linux is no where near ported to it because there's a lot more to a computer architecture than it's instruction set. Sure the porting would happen faster if Apple was more open about their chip, but the point is, Linux is almost certainly less portable than rust by a longshot

0

u/[deleted] Apr 15 '21

[deleted]

12

u/Shikadi297 Apr 15 '21

I'd like to avoid the term runtime, since you can argue C has a runtime if you count standard library implementations, and Rust's runtime is similar to that. (Or alternatively, neither Rust or C has a runtime, the term is ambiguous) If we're talking interpreter/VM, rust does not have that, it's a fully compiled language, allocations included, just like C and C++. Most of the cool benefits happen at compile time, and the rest of them are "Zero cost abstractions" in the same way that C++ containers are "Zero cost abstractions"

(I'm sure I'm over simplifying/leaving out other benefits)

→ More replies (4)

21

u/Dont_Think_So Apr 15 '21

The guarantees of Rust don't come from a custom allocator or runtime, they come from strict compiler checks. Certain classes of memory safety bugs that are made in C don't even compile in Rust, and because the checks happen at compile time there is no runtime penalty. Indeed, equivalent rust code can sometimes be faster than C because you don't need the checks at runtime.

→ More replies (5)

6

u/silmeth Apr 15 '21

how the rust memory allocator/deallocator

There is no such thing as ‘rust memory allocator’ – Rust-the-language or rustc compiler knows nothing about allocations by itself. Just like in C heap allocations are just calls to some function that allocates memory and returns a pointer to it to you.

And you can switch the allocator (Rust used to use jemalloc by default in the past, now it uses the default system allocator, but you can write your own if you wish, or use some external one) – or use the language without any allocator at all.

Rust borrow checker is independent of the allocator used and works both with heap and stack memory.

The Rust-for-Linux project for now wraps the kernel allocator in a Rust API so that the heap collections from standard library are usable, but they probably are going to write their own equivalent of std alloc (ie. the std library subset for heap-allocated collections) to make it impossible to not handle ouf-of-memory errors.

The Rust standard library – but not Rust the language – generally assumes that OOM errors are fatal and panics, ie. kills the thread when they happen – not giving the user any means to handle OOM at the call site; but that’s not a language restriction, and standard library also adds APIs for fallible allocations like Vec::try_reserve, so it’s also possible that kernel will use those if it’s added and stabilized quickly enough, and if they can switch off the infallible versions of functions (if eg. some compile flag or feature is added for this, or if they write their own lint catching uses of infallible allocations).

Similar strategy – rewriting heap collections for fallible allocations was/is used by Servo (and I think Rust parts in Firefox, taken from Servo?), so that the browser can handle OOM gracefully, not crashing on the user.

8

u/[deleted] Apr 15 '21

video card support should not be in the kernel because it's not absolutely necessary. your pc will run just fine without it.

5

u/[deleted] Apr 15 '21 edited Jun 11 '23

fuck u/spez

2

u/pdp10 Apr 15 '21

Anyone interested in modern microkernels should look first at seL4. Design priorities are security and performance. It's in C, but subject to formal verification.

Microkernels make it easy to write "servers" (subsystems) in any language you want, because there's no monolithic kernel to link. On the other hand, you need a stable object interface protocol between the microkernel and the subsystems. So far no microkernel has spawned an ecosystem of kernel-independent subsystem components -- not even the much-used Mach.

1

u/[deleted] Apr 16 '21

Who defines what is "absolutely necessary"

1

u/Booty_Bumping Apr 19 '21

This was never the guiding principle behind the kernel.

0

u/[deleted] Apr 15 '21

[deleted]

→ More replies (22)

0

u/[deleted] Apr 15 '21

[deleted]

0

u/[deleted] Apr 15 '21

[deleted]

17

u/cutchyacokov Apr 15 '21

I think you're looking at this the wrong way. I think the idea is, moving forward, some things will be written in Rust rather than C in kernel. There are both advantages and disadvantages to this, as listed in the RFC.

26

u/FaliedSalve Apr 14 '21

Ever use Rust? It seems cool and powerful and all, but the syntax seems backwards to me. I tried it, but it just seems ... I dunno... I can't get into it. The whole "fn" designation on functions and the "let X = 21" seems like a throwback to ASP or BASIC or something.

Some of the syntax seems C++ like, but other things.. I just don't know where they came from. Kinda wish it was more like C or Java.

101

u/zmxyzmz Apr 14 '21

The syntax in Rust is quite like a mix between C and functional languages like ML. For example, in ML functions are declared with the fun keyword, and types are declared with expression : type. Rust borrowed a lot of syntax like this. ML also uses the let keyword.

107

u/steveklabnik1 Apr 14 '21

Part of the reason we did is that it plays much nicer with type inference:

let x: i32 = 5;
let x = 5;

The overall form looks the same with the type inferred. (There are other reasons too but this one specifically is why a lot of newer languages are moving towards something like this rather than the C-style int x;.

23

u/FaliedSalve Apr 14 '21

yeah, I'm sure it's fine. Just strange to me.

What IDE/editor do you use? I'm guessing that hovering over a variable will display its inferred type, like it does in some other languages? That may help.

44

u/zmxyzmz Apr 14 '21 edited Apr 14 '21

Rust analyzer is one of the main language servers used in the Rust community. It has plugins available for most major IDE's and text editors like vim. It has many features, including type information.

3

u/segfaultsarecool Apr 14 '21

Do you know why they don't support IntelliJ IDEA? Is it just because it's a young project?

42

u/bkeeneykid Apr 14 '21

IntelliJ has their own rust support via plugins for Clion, IDEA or a few other of their IDEs. It’s comparable in most features to rust-analyzer.

5

u/krum Apr 15 '21

I use IDEA for Rust (as well as Java/Kotlin). Trying to get onto VS Code for Rust (which I use *a lot* for some other languages), but IDEA's workflows are just better.

→ More replies (1)

5

u/yawkat Apr 15 '21

I believe that the main dev behind rust-analyzer actually also built the intellij rust plugin.

1

u/MadRedHatter Apr 15 '21

What IDE/editor do you use? I'm guessing that hovering over a variable will display its inferred type, like it does in some other languages? That may help.

Yup.

The fainter text off to the right side is the editor automatically showing you the types, and in a couple of places the parameter names are shown to the left.

5

u/malicious_turtle Apr 14 '21

There's a setting in Intellij + the Jetbrains Rust plugin to inline types beside the variable, not just for single variables but iter chains as well.

1

u/segfaultsarecool Apr 14 '21

What settings do you have for the rust in intellij? Wanna get mine set up with everything that supports rust.

3

u/malicious_turtle Apr 14 '21

I actually just have the defaults I think and I'm almost sure the inline type is online by default.

1

u/segfaultsarecool Apr 14 '21

Dope! Eager to pick up rust

4

u/FaliedSalve Apr 14 '21

I will check out the plug ins. Thanks

2

u/steveklabnik1 Apr 14 '21

I personally use VS: Code with the vim plugin, and yeah, you can hover, or you can have it shown inline all the time if you prefer.

9

u/cris_null Apr 15 '21

The project lead of Kotlin agrees with you.

Types are moving to the right.

-1

u/[deleted] Apr 14 '21

Personally, I'm more a fan of something like i32 x = 5; - it reads just like laid out: "32-bit integer 'x' equals 5."

Opposed to "let 'x,' a 32-bit integer, equal 5."

Even dropping the type, the "let" variant seems... I don't know. Soft? Like saying please. No - X equals 5, dammit. I'm not letting you do anything, I'm telling you.

I have the same complaint about the phraseology in mathematics as well, respecting this way of talking.

None of this really matters though.

29

u/Fish_45 Apr 15 '21

This issue here is that you'd have to introduce something like the auto keyword for type inference, which I personally really dislike

1

u/Beneficial-Grass466 Apr 15 '21

Your comment clicked it in my brain why C++ has to do auto x = 1; for type inference where other languages created with type inference in mind chose not to do TYPE NAME = VALUE definitions, like Go's x := 1. Thank you.

16

u/[deleted] Apr 15 '21 edited Apr 15 '21

Personally, I'm more a fan of something like i32 x = 5; - it reads just like laid out: "32-bit integer 'x' equals 5."

That pattern is actually unusual in typical writing, e.g. "Pen BIC" vs "BIC pen". And I think this holds for code just as well. For instance, compare

SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack;

to

Stack: SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4>;

I find the second line to be far more readable than the first. And I think most people would agree.

The principle difference is whether or not you consider the type of the variable more important than that variable's name. In some situations, like your example, the variable name doesn't matter much. But in general, it is the variable name that is conveying the intent of the code, and the type is only a technical detail. For that reason alone, I think ML-style type declaration is preferable to C-style type declaration.

Anyway, at the end of the day it's just syntax. After a few weeks, it fades into the background.

17

u/ECUIYCAMOICIQMQACKKE Apr 14 '21

Contrary, I think the let phrasing sounds more commanding. Let there be light. Let x be 5.

I'm not letting you do anything

I interpreted it as you allowing the variable to take values, the variable can't do that without your permission.

1

u/mina86ng Apr 15 '21

Just don’t write the type. Problem solved. Now the lines read ‘let x equal 5’.

25

u/coderman93 Apr 14 '21

Yeah if anything the syntax is more modern than C and C++. Not the other way around.

24

u/mina86ng Apr 14 '21 edited Apr 15 '21

Here’s a puzzle: What does the code below do in C++:

#include <stdio.h>

struct Foo {
        Foo() { fputs("Hello, ", stdout); }
        ~Foo() { puts("world."); }
};

int main(void) {
        Foo foo();
        return 0;
}

Explicit keywords for function and variable declarations are a good thing. There’s also been a precedence for them in a language with C syntax — namely JavaScript — so I don’t see why it would be throwback to ASP or BASIC.

37

u/[deleted] Apr 14 '21

It clearly tells me:

  • Tie the programmer to an anvil and drop him in the Marianas Trench.

  • Format that disk and burn the backups.

6

u/thephotoman Apr 15 '21

This seems like one of my "Tank an interview because I've heard enough and no thanks" answers. It's on a level with my "fuck it, I'm going to implement FizzBuzz as a web service complete with a UI and accounts, persistence, and data import/export."

10

u/_Sh3Rm4n Apr 14 '21

In case someone was not able to solve the puzzle just by looking at the code, here is the answer: https://godbolt.org/z/hqjoWaKed

35

u/kazkylheku Apr 14 '21

This is also vaguely related to the problem in C++ that you might forget the object name! E.g. suppose you wanted to write:

{
   mutex_locker foo(mymutex);

}

but you forgot the foo:

{
   mutex_locker(mymutex);

}

Now it does nothing all; a temporary mutex locker is constructed which locks and unlocks the mutex, leaving the function body unprotected by the mutex, so that your program has a race condition.

In 2007 or so, I added the warning to GNU C++ to catch this. I worked as the kernel/toolchain/compiler/distro person in hardware startup company. The application devs used these kinds of classes and were running into this bug, so I rolled up my sleeves and hacked up the diagnostic.

Upstream never merged it.

4

u/batweenerpopemobile Apr 15 '21

I don't do a lot of c++, but that flag definitely looks like something that would be useful to have in g++.

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=36587#c11

The bug database has an "enhancement" type, so obviously, it is to be used for submitting enhancements.

It looks like you made an assumption about their process from a poorly named field in the bug tracker, years later someone wading through the bug tracker found your patch and asked you to submit it via the mailing list and you gave a speech about why their process should work how you think it should and then never bothered to submit it?

Looking at the 2007 Getting Started Page

https://web.archive.org/web/20071013020620/https://gcc.gnu.org/wiki/GettingStarted

In the "Basics" section it said

So, you are ready to contribute to GCC. We are constantly looking for new developers who are willing to donate their time to advance GCC.

Before you do, however, there is an important formality that you need to go through: Copyright assignment.

GCC is owned by the Free Software Foundation (FSF), as such, all contributors must assign their copyright to the FSF before any of their changes are accepted. The copyright assignment process is described in Contributing to GCC.

On the 2008 version of the contribute page it links to at

https://web.archive.org/web/20080623200532/https://gcc.gnu.org/contribute.html

it says

When you have all these pieces, bundle them up in a mail message and send it to the appropriate mailing list(s).

It does mention the bug tracker in the context of patches

A description of the problem/bug and how your patch addresses it.
For new features a description of the feature and your implementation. For bugs a description of what was wrong with the existing code, and a reference to any previous bug report (in the GCC bug tracking system or the gcc-bugs archives) and any existing testcases for the problem in the GCC testsuite.

I didn't see your name appear in the mailing list archives from a google search, and manually downloaded and greped June, July and August of 2008 since Google's search can be pretty shit these days for any kind of data spelunking.

============

Unrelated, I did see that you were on some of their other mailing lists, and appear to have been involved in the "egcs" fork back in the late 90's, which is pretty neat. I don't know if I was even online yet, back then.

8

u/KingStannis2020 Apr 14 '21
include <stdio.h>

struct Foo { Foo() { fputs("Hello, ", stdout); } ~Foo() { puts("world."); } };

int main(void) { Foo foo(); return 0; }

0

u/myaut Apr 14 '21

Most vexing parse is a well-known problem of C++, and it was partially fixed by adding brace-initializer syntax.

I've tried to check to Rust manual for syntax once again. No way I would like that syntax. Here is an example which would be uncomfortable for a newbie:

fn r#match(needle: &str, haystack: &str) -> bool {
    haystack.contains(needle)
}

WTF is r#? How does it change function semantics?

27

u/zmxyzmz Apr 14 '21 edited Apr 14 '21

The reason for the r# here is because match is a reserved keyword in rust which allows you to do pattern matching. For example, rust has sum types /enums which you can match on:

match foo_result {
    Ok(r) => do something with the result r,
    Err(e) => do something with the error e,
}

You can only call a function or variable match if you specify that it is to be treated by the compiler as a raw string by prefixing with r#. In other words, this is a bad name to call your function.

If you want to look at more realistic examples, you could take a look at something like Rust by Example.

0

u/[deleted] Apr 14 '21

[deleted]

12

u/zmxyzmz Apr 14 '21

So we get to write garbage C++ to trash it and evangelize Rust, but not vice versa?

I've not said anything about C++, only clarified the syntax in the above code example.

2

u/sophacles Apr 16 '21

Rust haters argue like republicans: facts don't matter unless they let the hater win, and if they can't find facts to win with they lie, and if called out on a lie they just complain about being a victim.

5

u/[deleted] Apr 15 '21

Can you provide an example where the Rust code looks good and simple but has surprising bad behavior?

→ More replies (2)

9

u/Helmic Apr 15 '21 edited Apr 15 '21

I'm not sure how it's trashing Rust, to be honest. Someone tried to code like a jackass in Rust and was forced to spell out that match here is a function name and not the keyword. Whoever has to read the code later is probably pretty thankful and can yell at whoever wrote that for taking the extra effort to write out r# instead of using a better name.

But if you have to use "match" for whatever reason, like you can still go for it. You just need to clarify it's not the match keyword.

4

u/ReallyNeededANewName Apr 15 '21

This is like trying to call a function switch in C++. The difference is that Rust apparently has an escape for it

15

u/silmeth Apr 14 '21

How does it change function semantics?

It doesn’t at all. It just lets you call the function match which is a Rust keyword. Try defining a C function called switch() – what you pasted is this kind of thing, just in Rust.

Most languages won’t let you use reserved keywords as identifiers. Rust does, r# is an escape hatch if you eg. need to call an external function (from a C library, perhaps) named eg. match. Or if you want to use a library written in an older version (‘edition’) of Rust which used some name that wasn’t reserved then (eg. try was a valid identifier in Rust 2015, but is not in Rust 2018) – Rust guarantees that even though you use newer version of the language, you are still able to compile and call the old code written using the old syntax without problems.

But native Rust code will (almost) never use this name because it clashes with a keyword and it’s just easier to chose some different name. (To be fair, before seeing your comment I wasn’t aware you could use r# in identifiers… never seen it used at all.)

14

u/mina86ng Apr 14 '21 edited Apr 15 '21

Let me rephrase your argument:

I’ve tried to check C++ manual for syntax once again. No way I would like that syntax. Here is an example which would be uncomfortable for a newbie:

auto cmp = [=](auto &&el) { return el < x; };

WTF is [=]? WTF is &&? How does it change semantics of whatever cmp is?

You may dislike Rust syntax but in comparison of whose syntax is more readable, C++ looses with all commonly used languages.

Frankly, claiming that the code you’ve posted would make a newbie uncomfortable is laughable. r# is the only confusing thing there (and to answer your question it makes it so that match, which is a keyword, can be used as an identifier) and the rest is arguably more readable than C++’s function definition.

PS. Amount of questions about most vexing parse on the Internet seems to suggest it’s not a well-known problem of C++. Meanwhile, the partial fix using brace-initialiser prompts its own puzzle: What does the following C++ program output:

#include <iostream>
#include <vector>

int main(void) {
        std::vector<int> a(6);
        std::vector<int> b{6};
        std::vector<int> c({6});
        std::vector<int> d(6, 9);
        std::vector<int> e{6, 9};
        std::vector<int> f({6, 9});
        std::cout << a.size() << ' ' << a[0] << ", "
                  << b.size() << ' ' << b[0] << ", "
                  << c.size() << ' ' << c[0] << ", "
                  << d.size() << ' ' << d[0] << ", "
                  << e.size() << ' ' << e[0] << ", "
                  << f.size() << ' ' << f[0] << '\n';
        return 0;
}

5

u/dreamer_ Apr 15 '21

Tip: Using three backticks does not work in all versions of reddit. Format blocks of code using 4 spaces, please - otherwise your post is unreadable. It's stupid, I know.

6

u/KingStannis2020 Apr 15 '21

I wish Reddit would quit trying to make "New Reddit" happen. The day they shut off the old UI is the day I quit using this website.

2

u/ReallyNeededANewName Apr 15 '21

Or a tab. Tabs actually work too

→ More replies (11)

3

u/kazkylheku Apr 14 '21 edited Apr 14 '21

That's tricky! If it was Foo foo, it would obviously be constructing the object, so the constructor and destructor get called.

But, now, can you have parentheses on the object being declared if there are no constructor arguments (you are using the default constructor?) It's been a while, sheesh.

Ah right, I remember; indeed, no you cannot. Foo foo(); is actually a declaration that there exists a function foo which returns Foo, and takes no arguments. This program prints nothing.

If you remove the (), you get hello, world.

You have to include <stdio.h>, which is outdated; in C++ you are supposed to use <cstdio>, and then puts and whatnot will be in the std:: namespace.

→ More replies (5)

2

u/DerfK Apr 15 '21

Foo foo(); is obviously wrong to those of us that last touched C++ in the early 90's. This clearly should have been Foo foo = Foo();

Speaking of which, in the event that I ever want to touch C++ again, is there some guide to the changes in C++. Starting whenever they added namespaces (looks like 1995).

3

u/[deleted] Apr 15 '21

If you didn't touch C++, you can just as well go and treat it like a new language. A LOT of things change with and since standardisation.

4

u/kazkylheku Apr 15 '21

Foo foo = Foo(); pointlessly constructs a temporary object, and then copy-constructs it to initialize foo.

This will not work if Foo has only a default constructor and no copy-constructor. (IF you write a default constructor, that will suppress the compiler-generated copy-constructor from existing).

C++ compilers are allowed to optimize that and turn it into Foo foo; thereby eliminating the temporary.

That, even if there are side effects in the copy constructor that thereby disappear!

Before C++, I never heard of a language whose optimization was allowed to eliminate side effects written by the programmer.

2

u/robin-m Apr 15 '21

That's not true anymore since C++17 with guaranteed copy elision. A non-copyable, non-movable type can use Foo foo = Foo(); because no copy nor move will be called.

29

u/dreamer_ Apr 14 '21

let and fn is there from OCaml's let and fun. It's overall better syntactic decision than C++ syntax - it's easier to write, parse, and understand.

20

u/[deleted] Apr 15 '21

This seems like an oddly parochial viewpoint to me. Have you ever used an ML-family language; e.g., OCaml, Haskell, Scala?

17

u/phire Apr 15 '21

Interesting. I had no problems with the syntax.

My main language is c++ and I found every syntactical change to be a logical improvement. I quite enjoyed it.

Where I ran into massive problems was getting the type system and ownership to do what I wanted. I felt like I was continually fighting it, with it always mockingly pointing out thst my data structure and control flow was fundermentally flawed.

I found I had to rewrite things several times before I stumbled across something that would actually compile. It really slowed me down in getting my ideas into code.

On the plus side, once it did compile, it's almost always worked. But I moved back to c++.

I'll probably try rust again sometime in the future, once it has a few more features in the type system. I want to see if I can make my mental model match rust's model.

22

u/delcontra Apr 15 '21

This behaviour is usually called "fighting the borrow checker" by the Rust community, and it's normal for begginers and one of the reasons that people usually describe Rust as having a high learning curve. But I promise you it gets better with time, and may even help build safer code in other languages since you're forced to think about memory safety, data races, etc

14

u/BigChungus1222 Apr 14 '21

The syntax is the least important part of a language to me as long as it isn’t very much in my way.

4

u/iheartrms Apr 15 '21

Amateurs worry about syntax. Professionals worry about semantics.

3

u/kazkylheku Apr 14 '21

LET was actually BASIC trying to be Lisp!

fn could be inspired by Paul Graham's Lisp dialect Arc, in which he shortened lambda to fn. C++ has that now and spells it lambda.

14

u/mina86ng Apr 14 '21

Uhm, no. C++ spells lambda as […](…) { … }.

1

u/[deleted] Apr 15 '21

With the () being optional in newer standards when it doesn't have any parameters. Also, you can have <...> between ] and ( if you want a template lambda.

2

u/Shikadi297 Apr 16 '21

lol that sounds so gross when you put it like that

1

u/[deleted] Apr 16 '21

yeah, you can literally type something like auto func = [&]<typename T>(const T &t) { std::cout << t << std::endl; };

but you can also write auto func = [&](const auto &t) { std::cout << t << std::endl; };

both versions mean the same; and yes, you can use auto also for normal functions and methods these days instead of <typename T>

7

u/steveklabnik1 Apr 14 '21

It wasn't inspired by Arc. In ancient times, there was a rule that keywords had to be under five characters, so "function" and "lambda" wouldn't work. `fun` is the only real alternative, but `fn` was chosen.

That restriction has been relaxed, we have `continue` these days, rather than `cont`. But the most often used keywords remain short.

1

u/Narishma Apr 15 '21

A lot of old BASICs used fn (or rather FN) for function definition and function call.

1

u/kazkylheku Apr 15 '21

Ah, you're right! DEF FN and all that.

1

u/DhavesNotHere Apr 15 '21

I've been trying to learn it off and on for years. I have so many stupid questions and I don't even know what vocabulary to use to ask them. I'd really like to know the "whys" for why things are done various ways. The Rust book and this other one I read just say you have to do things like use options in various places or all of that type stuff and I am just mystified.

But I guess that's because I main python, which is pretty much the ass-opposite of Rust.

6

u/steveklabnik1 Apr 15 '21

Please post those questions to the weekly question thread in /r/rust, or come by the Discord. Happy to answer.

0

u/[deleted] Apr 14 '21

That sort of notation is more along the mathematical way of things. I have no experience with Rust myself, though.

("let" is also common in LISPs as well, I think?)

0

u/[deleted] Apr 14 '21

[deleted]

9

u/nomenMei Apr 15 '21

I agree that syntax is mostly superficial, but saying that "C syntax is literally 50 years old, it should not be the standard for anything" directly contradicts that.

C syntax has maintained for 50 years not just because it is familiar (I definitely didn't grow up writing ANSI C) but also because there are things about it that resonate with programmers, old and new.

11

u/ffscc Apr 15 '21

C syntax has maintained for 50 years not just because it is familiar ... but also because there are things about it that resonate with programmers, old and new.

This is a poor line of reasoning as well. You'd be hard pressed to find anyone extolling the virtues of SQL syntax, despite the fact that it's also nearly 50 years old and arguably more prevalent than ever. Even the NoSQL vendors all ended up implementing an SQL-like query language. But that still doesn't prove that SQL syntax "resonates with programmers, old and new".

C syntax is appealing because it is familiar. Even if someone didn't grow up on C then they probably grew up on a language with C-like syntax (perl, PHP, C++, Java, C#, etc). And those languages adopted C-like syntax because of people like the one above.

1

u/Ar-Curunir Apr 15 '21

The only that about C that resonates with programmers is that C has has managed to infect many systems, because it was the only language in its niche for a long time. That’s changed now with Rust, and in the future with Zig, Nim, etc

1

u/nomenMei Apr 15 '21

I've never heard of Zig and Nim, I'll be interested in seeing their progress going forward.

13

u/kazkylheku Apr 14 '21

Math notation is hundreds of years old; we should stop writing cruft like

 2       2        2
a   +   b    =   c

Good argument!

38

u/IceSentry Apr 14 '21

Do you think that was the first ever notation for exponents? It literally took humanity multiple centuries to standardize on arabic numbers and that exponent notation. According to wikipedia, this notation was first used by Descartes in the 17th century, but we've been aware of exponents since ancient greece. Computer science as a field is not even 100 years old yet.

source: https://en.wikipedia.org/wiki/Exponentiation#History_of_the_notation

0

u/DhavesNotHere Apr 15 '21

Newton invented Calculus with geometrical methods, not algebra.

8

u/IceSentry Apr 15 '21

Ok? Does that change anything about what I said on exponent notation and how it changed over time?

5

u/DhavesNotHere Apr 15 '21

No, I'm pointing out that that changed over time as well.

2

u/IceSentry Apr 15 '21

Ok, that's fair, I assumed your comment was more confrontational than agreeing.

10

u/wobblyweasel Apr 14 '21

there was a shit ton of different math notations, also math is like thousands of years old

0

u/[deleted] Apr 14 '21

[deleted]

18

u/KingStannis2020 Apr 14 '21

Is there anything in particular that you struggle with? Things that could be better explained in the documentation, etc.

17

u/dreamer_ Apr 15 '21

Seriously, "I don't like the syntax" is the lamest and the most surface-level criticism of any language. Experienced users know why the syntax is it is, and why it is better than C++ (for newbies reading this: C++ syntax is being designed around "what combination of symbols can we use that is not a valid C++ program yet", not around readability).

Sure, there are languages with terrible syntax (CMake comes to mind), but programmers just shrug and carry on. I don't think anything syntax-related in the documentation needs to be explained better :)

7

u/call_me_arosa Apr 15 '21

Syntax is pretty much just getting used to it. You can argue that some language are too verbose or unclear but after using any language for more than 2 or 3 months you usually don't even think about it anymore.

9

u/GenericAntagonist Apr 15 '21

Syntax is pretty much just getting used to it.

Yes and No. You can get used to a lot of things (though it can make language swapping hard if you get used to something like go where everything is declared backwards) and that definitely helps, but syntax can also create monsters. The well known

void (*signal(int, void (*fp)(int)))(int);

declaration from C comes to mind immediately as a situation where even trained professionals will struggle to grasp what is going on. Likewise the syntax of languages like malboge or brainfuck is so unlike other languages (deliberately obviously) that it can take years to get a simple program together in them.

2

u/Shikadi297 Apr 16 '21

Yeah I have no idea what that's doing =D

I think that would fall under ergonomics over syntax though. Of course, I don't even know if there's a strong distinction between the two, but the way I see it is that syntax is only required because there wasn't a better way to do it. If for example the syntax in C++ required to do that is cleaner, you would still be able to write that abomination if you wanted to, which wouldn't be the syntax's fault.

Brainfuck on the otherhand, that crosses the line into syntax mattering quite a bit lol

0

u/[deleted] Apr 15 '21

[deleted]

2

u/Shikadi297 Apr 16 '21

I think it's a big part for less experienced/beginner programmers to choose one language over another, and it's probably weighed in the decision when choosing similar languages (e.g. Python vs Ruby, Java vs C#) but usually it's a small weight, and the features, ecosystem, and ergonomics of the language are what matter most

→ More replies (16)

5

u/[deleted] Apr 15 '21

Python's syntax for type hints is very similar to Rust's variable declarations.

1

u/Shikadi297 Apr 16 '21

And yet it's useless in comparison because of how much better Rust's type system is

(Better is relative, also I didn't say it's useless in general)

28

u/[deleted] Apr 15 '21

Is Linux the only system playing with rust at such a low level? Microsoft has a shitton of legacy support to worry about and while Apple might be experimenting with it underground somewhere, that doesn’t mean we will ever see it.

And then there’s the BSD guys. I don’t imagine they would take much interest in this.

34

u/[deleted] Apr 15 '21

Microsoft to my knowledge also pushes for Rust (including for use in their own kernel).

9

u/writtenbymyrobotarms Apr 15 '21

e.g. the IoT Edge client has a fair bit of Rust in it.

21

u/frymaster Apr 15 '21

Microsoft has a shitton of legacy support to worry about

In this context, so does Linux - however much userspace might change, they pretty much never break userspace with kernel changes. MS would likely do the same thing Linux is likely to do - use it for new things and for targeted rewriting of existing things

13

u/NynaevetialMeara Apr 15 '21

Android is rewriting some of their stack in rust. I imagine that will eventually involve some kernel modules as well.

4

u/KingStannis2020 Apr 15 '21 edited Apr 15 '21

I believe a while back Apple had posted some job openings for a team that was going to write filesystem code in Rust.

1

u/[deleted] Apr 15 '21

So it’s possible APFS has some rust in it? Interesting

80

u/BigChungus1222 Apr 14 '21

It looks like google is replacing significant parts of android with Rust implementations as well. We might be moving in to a more secure era of computing.

40

u/_herrmann_ Apr 15 '21

Don't hold your breath

11

u/[deleted] Apr 15 '21

they are working on that Fuchsia thing, so maybe that's a convergent effort?

10

u/I_AM_GODDAMN_BATMAN Apr 15 '21

wanna bet they're doing these without coordinating with each other?

1

u/_herrmann_ Apr 15 '21

Nevermind me. I'm just a grumpy old man. Sarcastic cynical, open wound poking dickhead. Tbh I truly think we can do better, but my reality check brain says, "nah, no way dude. Shit only gonna get worse."

→ More replies (8)

8

u/Sufficient_Lime1529 Apr 15 '21

I have followed the journey since the first kernel was put up for ftp so nothing surprises me.

The core philosophies underpinning Linux kernel development as expressed by Linus.

==================== ==================================================

"The only constant in Linux is CHANGE".

To a corporate gathering asking about a roadmap "Linux is evolution, not intelligent design".

8

u/Brisprip Apr 15 '21

After all these years, maybe I can finally submit my first patch to the kernel;)

1

u/LibreTan Apr 19 '21

Is it a good idea to introduce a new (not yet mature compared to C) language to a project like the Kernel which needs to be stable and scalable for years to come?

-52

u/i_meant_lulz Apr 15 '21

Linus disappoints me with accepting immature programming languages that doesn't even have stable releases for enterprise use yet. He used to have better judgement back in the days. I hope he comes to his senses before the kernel takes a turn into a path of no return and we're gonna need very recent hardware to compile the kernel properly because compilation takes double the time to compile, 100% of systems resources, just because of "the complex features built in the language" blah blah. This virus starts with a module or driver and watch it as it will head to core kernel functions. Guaranteed. Dark days of open computing are ahead.

20

u/dreamer_ Apr 15 '21

(…) immature programming languages that doesn't even have stable releases for enterprise use yet.

Rust does have stable releases (called editions). And it sees plenty of enterprise use. I wouldn't be surprised if there's more enterprise Rust code than open-source Rust code out there. But anyway - it does not and should not matter to the kernel, only technical merits.

11

u/_Sh3Rm4n Apr 15 '21

Every rust release is stable, which means it does keep backwards compatibility to all versions before according to the semver scheme.

Technically editions are just breaking points, which can introduce breaking changes for e.g. the syntax with an opt-in system (using the new edition) while still being able to mix code written in the old edition with code written in the new edition.

10

u/throwaway6560192 Apr 15 '21

more compilation time = death of open computing?

bit of a stretch IMO

6

u/[deleted] Apr 15 '21

maybe it will, but poor quality code will never be allowed in the kernel.

gcc planned a partial rewrite to c++ and that made a hard line about what features were allowed. mostly they cared about vectors because those would greatly simplify the gcc codebase. i actually do not know what came out of that effort.

i would expect the kernel to be no different. there are already some notes from Linus what is allowed and what is not.

1

u/i_meant_lulz Apr 19 '21

I hope so. Fingers crossed.

3

u/[deleted] Apr 15 '21

It is just funny how people can only focus on only negatives and be resistant to any change.

1

u/[deleted] Apr 15 '21

Besides productivity while working on the source code when compilations takes long, what exactly is bad about long compilation times?

5

u/continous Apr 15 '21

I mean, it's in the name. Long. Generally, you don't want something to take longer for no significantly good reason.

10

u/[deleted] Apr 15 '21

Well, the reason here is that the compiler checks for more errors. Isn't that a good reason?

2

u/continous Apr 15 '21

Theoretically, but if it only catches/solves one error for a 5-10x increase in compile time, that's really not a good position.

7

u/[deleted] Apr 15 '21

I think the "how much" depends on yes, the amount of errors, but also what kind of errors. Some are pretty easy to catch (e.g. forgotten semicolon) and some are harder (e.g. buffer overflow). And harder errors obviously take longer.

→ More replies (2)

10

u/[deleted] Apr 15 '21

You would rather hunt down obscure bugs later in development than it to never happen?

"Why should I write tests which just increase development time..."

1

u/continous Apr 15 '21

I would rather hunt down obscure bugs now, then try and implement an entirely new feature set that will hopefully find them.

2

u/IAm_A_Complete_Idiot Apr 16 '21

Safe rust guarentees no undefined behavior and memory issues. It's not a hopefully. If you used safe abstractions someone already made in the kernel, UB shouldn't be a thing.

→ More replies (2)

-10

u/Gaarco_ Apr 15 '21

Look at the toxic rust fanboys downvotes lmao

15

u/[deleted] Apr 15 '21

I mean, is it toxic fanboys who make his comment look bad or the fact he probably has no idea what he is talking about?

As you can see in other comments, there are more than enough people who can see the pros and cons about making Rust possible in Kernel development.

He just wants to trigger people.

-4

u/Gaarco_ Apr 15 '21

I mean, is it unusual to see people being downvoted just because they were against the rust fanboys opinion?

As you can see in other threads, there are more than enough people who are downvoted just because they were against the rust fanboys opinion.

The rust community is toxic.

4

u/[deleted] Apr 15 '21

Usually the downvotes I see for Rust related topics are the ones, that generalize or don't have any real foundation for a discussion. Also there are many trolls who just shit on Rust for the fun of it. I suggest you look at the comment history of some Rust haters. It's basically all some people do to create tension where there is none needed.

0

u/[deleted] Apr 15 '21

[deleted]

17

u/UltraPoci Apr 15 '21

Yeah, it's so stupid to be excited about new things in an area we are interested in. We should all program using Assembly and pen and paper. That's the way

10

u/etoh53 Apr 15 '21

They are the ones going to enter the workforce. Let them choose what they want.

13

u/throwaway6560192 Apr 15 '21

True. I can't think of any other possible benefits Rust might have except for novelty. $latestgeneration amirite?

0

u/[deleted] Apr 15 '21

[deleted]

5

u/[deleted] Apr 15 '21

Please don't stalk users.

-15

u/continous Apr 15 '21

I don't think this is a good idea. I mean, there's a reason the kernel doesn't include languages like Go or C. Things can and do go wrong. And the kernel needs to be as free of that as possible.

5

u/BCMM Apr 15 '21

there's a reason the kernel doesn't include languages like Go or C.

Can you expand on this?

→ More replies (8)

4

u/throwaway6560192 Apr 15 '21

Things can and do go wrong. And the kernel needs to be as free of that as possible.

What kind of "things" are you expecting to go wrong here?

→ More replies (14)

0

u/[deleted] Apr 15 '21

[deleted]

7

u/arbitrary_h_sapien Apr 15 '21

Care to elaborate for a noob?

4

u/rl48 Apr 16 '21

They are having a dandy little flame war about Rust versus C++ and C. It's a mess.

→ More replies (1)

0

u/[deleted] Apr 14 '21

[deleted]

17

u/soruh Apr 14 '21

We decided to go with Rust's idiomatic style, i.e. keeping rustfmt defaults. For instance, this means 4 spaces are used for indentation, rather than a tab. We are happy to change that if needed -- we think what is important is keeping the formatting automated.

→ More replies (9)

11

u/Zethra Apr 14 '21

If you're not familiar with Rust's tools. The official former does default to 4-space but that can be changed to tabs or whatever.

→ More replies (3)

10

u/inamestuff Apr 14 '21

I have to agree, very dumb choice.

I mean, even if the Rust team has a very weird fetish preference for 4-space indentation, that shouldn't be promoted as the default.

I've seen endless discussions about how many spaces should be used when the solution is as simple as "use a semantically meaningful tab", so that anyone can pick their preferred value. And I'm not even taking into account accessibility...

25

u/Unicorn_Colombo Apr 14 '21

I will take default 4-pace identation any time, thank you.

I had to wade through code that used what it looked like a random indentation. There was 3, 4, 7 and probably others. And that was Python of all things.

18

u/jarfil Apr 15 '21 edited May 12 '21

CENSORED

→ More replies (1)

9

u/[deleted] Apr 14 '21

I use a tabstop of 4 or 6 depending on my font.

It's like people don't even consider that not every monospace font has the same width.

11

u/inamestuff Apr 14 '21

Same, and I also used to indent with a width of 4 or more, but currently I'm using a width of 2, just because of personal preferences. No changes to the source code required, just a simple setting in the IDE/Text Editor and that's it - as it should be

→ More replies (3)
→ More replies (14)

6

u/dreamer_ Apr 15 '21

I've seen endless discussions about how many spaces should be used when the solution is as simple as "use a semantically meaningful tab", so that anyone can pick their preferred value. And I'm not even taking into account accessibility...

Personally, I prefer tabs and do exactly this, but you need tooling in place to make the code correctly formatted, otherwise it's a hassle and a source of pointless discussions. Aside of this - StackOverflow polled the users a few years ago, something like 80% of programmers in the wild avoid/don't use tabs altogether.

→ More replies (5)