1. 29

  2. 6

    What is the benefit of adding const to a function? Its surprising to see what feels like a relatively minor change make the release notes.

    1. 17

      Adding ‘const’ to a rust function makes it possible to execute the function in a “const context”. Const contexts are those which are computed at compile time. A const variable’s initialization is one example of a const context. Having more things computed at compile time can have significant effects on the run time behavior of code, effectively eliminating the run time cost of producing such a value, which mattes a lot when that code may be in a tight loop.

      The other thing to know is that the const feature was added to rust after much of the standard library was written. In fact the language is in the process of making more and more expressions have the ability to be const. There are even some typically heap allocated values which can be const evaluated. As those features get added the standard library is adding const to functions which can be made const.

      So although I don’t write the release notes and I don’t know the person who does. My guess would be that they appear there because it’s a way of tracking const’s progress as an important language that has serious runtime benefits.

      1. 1

        Hm interesting, so it’s basically constexpr from C++?

        I started using constexpr recently, and I noticed how it has progressed over C++11, 14, 17, 20. At first you could only use functions with a single return statement. Now they are adding certain STL algorithms, etc.

        That is, making more and more language features valid in constexpr contexts until the STL itself can run at compile time. And probably recasting the STL itself in terms of newer features that can run in both contexts. (Memory allocation is an issue there too, but I don’t remember the exact mechanisms)

        Sounds like Rust is undergoing a similar evolution?

        1. 7

          Const contexts are those which are computed at compile time.

          I believe it more that they can be compiled at compile time, but whether they are or aren’t is up to the optimizer. But it allows the optimizer a lot more leeway in how things work; a const function is basically pure, so a lot more reduction can be done.

          Hm interesting, so it’s basically constexpr from C++?

          Pretty much exactly that, yes. Rust’s const is currently less powerful than constexpr, but it is kind of slowly creeping up on feature parity.

          1. 2

            I think const functions can also be used in types in ways that non const functions cannot. But I don’t remember all the details

    2. 2

      speaking of additions to the language, any idea why a crate for a proprietary password manager is now required to build rust itself?


      1. 9

        That particular section of Cargo.toml is listing all of the workspace members, which just means that all of those projects share dependency resolution information, compilation profiles, etc.

        That particular project is part of cargo, and it looks like that’s just one particular provider for storing tokens securely. There are other crates in the same repository for macOS, Windows, and Gnome. I suspect the maintainers would be open to contributions that support other providers.

        1. 2

          fair enough, thank you for filling me in

          1. 1

            1password is no more proprietary a password manager than macOS or Windows.

            1. 1

              You’re correct of course that macOS and Windows are both proprietary, but if you buy a Mac or Windows laptop, 1Password is not included in that purchase :)

          2. 4

            These tools are described in the cargo-credential README.

            The specific crate you mentioned contains this single file: main.rs

            It seems to be a wrapper around 1password’s op command line tool, implementing the Credential trait for storing and retrieving passwords.