or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
cg vision doc
motivating examples
[T; N]
for anyN
[T; N]: Default
for allN
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →serde
)xtensor
.fn split_first<const N: usize>(a: [u8; N]) -> (u8; [u8; N - 1])
key points and questions
trait Foo { const N: usize; }
trait Foo { const fn compute(&self) -> usize; }
impl Something for Foo { const fn compute(&self); }
const
?struct Foo<const F: fn()>
orstruct Foo<T: Fn()>
or something.0
andN > 0
(for unsigned integers).status quo stories
Alan/Grace/Barbara wants to parameterize her type with enums
https://rust-lang.zulipchat.com/#narrow/stream/260443-project-const-generics/topic/meeting.202021-04-27/near/236964582
Alan wants a maths library using arrays
https://rust-lang.zulipchat.com/#narrow/stream/260443-project-const-generics/topic/meeting.202021-04-27/near/237079733
Barbara wants to implement
Default
for all array typesBarbara is working on
std
. She wants to implementDefault
for all array types. Currently, it is implemented forN <= 32
using a macro (link)She thinks "Ah, I can use min-const-generics for this!" and goes to write
So far so good, but then she realizes she can't figure out what to write in the body. At first she tries:
but this won't compile:
"Ah," she realizes, "this would be cloning a single value of
T
, but I want to makeN
distinct values. How can I do that?"She asks on Zulip and lcnr tells her that there is this
map
function defined on arrays. She could write:"That code will build," lcnr warns her, "but we're not going to be able to ship it. Test it and see." Barbara runs the tests and finds she is getting a failure. The following test no longer builds:
"Ah," she says, "I see that
Default
is implemented for any type[T; 0]
, regardless of whetherT: Default
. That makes sense. Argh!"Next she tries (this already relies on a nightly feature)
While this does seem to compile, when trying to use it, it causes an unexpected error.
The compiler can't tell that
N == 0 || N != 0
is true for all possibleN
, so it can't infer[T; N]: Trait
fromT: Trait
.Frustrated, Barbara gives up and goes looking for another issue to fix.
Even worse, Barbara notices the same problem for
serde::Deserialize
and decides toabandon backwards compatibility in favor of a brighter future.
Barbara wants to replace her uses of
typenum
with const genericsTODO: write this
Barbara extends rust-gpu with
Image
typesshiny future stories
Barbara wants to implement
Default
for all array typesBarbara is working on
libstd
. She wants to implementDefault
for all array types. Currently, it is implemented forN <= 32
using a macro (link)She goes to write
or alternatively, uses specialization to write something like
rust-gpu shiny future
Barbara is working on rust-gpu. In that project, she has a struct
Image
that represents GPU images. There are a number of constant parameters allowing this type to be heavily customized in a number of ways. In some cases, helper methods are only available for images with particular formats. She can represent at compilation time using associated constants:Evaluatable
Version A: Evaluatable
Barbara is working on her project. She has the idea to write a
split_first
function that will allow her to split out the first item from a fixed-length array; naturally, the array must be non-empty. It looks something like this:Next she wants to write a function that uses
split_first
:The compiler gives her an error message:
Barbara hits the "quick fix" button in her IDE to add the where clause.She immediately gets an error at another spot:
Notes:
split_first
has an implied where clause based on its return type thatN-1
must be evaluatable.N-2
in the bounds does not allow you to computeN-1
, even though there are no failure conditions whereN-1
could fail butN-2
couldn'tVersion B: Smarter compiler
Barbara is working on her project. She has the idea to write a
split_first
function that will allow her to split out the first item from a fixed-length array; naturally, the array must be non-empty. It looks something like this:Next she wants to write a function that uses
split_first
:The compiler gives her an error message:
Barbara hits the "quick fix" button in her IDE to add the where clause. She immediately gets an error at another spot:
Notes:
split_first
has an implied where clause based on its return type thatN-1
must be evaluatable.N > 0
is true allowsN - 1
to be evaluated.Version C: Monomorphization errors
Barbara is working on her project. She has the idea to write a
split_first
function that will allow her to split out the first item from a fixed-length array; naturally, the array must be non-empty. It looks something like this:Next she writes a function that uses
split_first
:Everything seems fine when she runs
cargo check
. Then later she runscargo test
and sees a compilation error:Notes:
N-1
is not detected until we monomorphize.