Git Product home page Git Product logo

const-eval's Introduction

Constant Evaluation

Constant evaluation is the process of running Rust code at compile-time in order to use the result e.g. to set the discriminant of enum variants or as an array length.

Examples:

enum Foo {
    A = 5,
    B = 7 - 3,
}
type NineStrings = [&str, 3 * 3];

The Rust compiler runs the MIR in the MIR interpreter (miri), which sort of is a virtual machine using MIR as "bytecode".

Table of Contents

Related RFCs

Const Promotion

RFC 1414 injects a hidden static for any &foo expression as long as foo follows a certain set of rules. These rules are discussed here

Drop types in constants/statics

RFC 1440 allows using types that implement Drop (or have fields that do so) inside constants and statics. It is guaranteed that the Drop::drop method will never be called on the static/const object.

Constants in Repeat expressions

RFC 2203 allows the use !Copy types for the initializer of repeat expressions, as long as that value is constant.

This permits e.g. [Vec::new(); 42].

Statically known bugs and panics in runtime code are warnings

RFC 1229 formalized the concept that

let x: usize = 1 - 2;

is allowed to produce a lint but not an error. This allows us to make these analyses more powerful without suddenly breaking compilation. The corresponding lint is the const_err lint, which is deny by default and will thus break compilation of the crate currently being built, even if it does not break the compilation of the current crate's dependencies.

Various new const-eval features

Some of these features interact. E.g.

  • match + loop yields while
  • panic! + if + locals yields assert!

const-eval's People

Contributors

oli-obk avatar ralfjung avatar ecstatic-morse avatar chrissimpkins avatar lingman avatar johntitor avatar rchaser53 avatar varkor avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.