Conversation

Replying to
One thing that does confuse me in twitter.com/mcclure111/sta: `fn addone(x:i32) -> i32 { x + 1 }` is "obviously" a const function but Rust does not allow me to treat it as such unless I decorate it const. Is there a linter that will catch "you forgot to const this fn"? Can "Clippy"?
Quote Tweet
ME: Ugh I found another error in the spec, it says you can use ANY const expression for the length of an array type but clearly that can't be right, every language has stricter constraints on expressions in that position ME: *tests it* *it compiles* ME: … …  … nice
Show this thread
An array is a fixed-size sequence of N elements of type T. The array type is written as [T; N]. The size is a constant expression that evaluates to a usize.
const fn addone(x:i32) -> i32 {
	x + 1
}

fn main() {
	let array: [i32; addone(3) as usize];
}
4
4
Are there potential performance or other downsides to not explicitly decorating a function a function as const when it, in actual fact, happens to be const?
5
4
A really weird problem I keep finding I am having in Rust is writing "fun" instead of "fn", basically every time
2
21
By the way, something I don't understand. So it lets me do this. "array" is unused, it gives me an unused var warning. but… it *can't* be used?! I didn't initialize it, you can't read an uninitialized var, and it isn't mut so I can't write into it. Why does it LET me define it?
Image
3
6
This statement in the reference for "slice" is what I want, but also it is bad. Are there (safe) steps I can take to reliably ensure bounds-checking for a particular use of a slice is removed before runtime? Are the rules for this documented somewhere (such as the rustc manual)?
Image
6
4
Like what I'm looking for is some way to type or decorate a site such that I'm asserting "I think I followed the rules to get a no-boundcheck optimization here, and if I messed up, I want the compiler to tell me so"
Image
Image
4
8
Several people replying "use iterators". Good approach, but here's a complication: I often find myself in a situation where I have 2 arrays, incidentally of the same length. int a:[T], b:[T]; I want to iterate over "both at once". For example produce a c such c[i] = a[i] + b[i]
1
7
Using the iterator syntax (which remember, I want to use because I want the language-level guarantee I'm avoiding bounds checks), is there a way to iterate over 2 arrays (or iterate up to end of shorter array)? In C I'd do this by iterating over key range and random indexing.
7
5
Replying to
If it happens to be the case that the standard library iterators will always follow the code path that removes the bounds checks at individual index sites, and this is a strong and universally understood social contract, that's "as good as" a lang guarantee for now i think
4
3
Show replies