generics-traits/const-generics #208
Replies: 48 comments 25 replies
-
您好,练习3的题解是不是有错误,check_size无法传入String类型,因为要求String类型实现copy trait,题解中的答案会报错,需要传入String的引用好像才可以通过编译 |
Beta Was this translation helpful? Give feedback.
-
我不理解,第一题的题解有什么意义呢。(似乎并不是真正解决问题) |
Beta Was this translation helpful? Give feedback.
-
第三题报错 |
Beta Was this translation helpful? Give feedback.
-
done!请问第七题得sqrt方法要T实现什么trait? impl Point { fn main() { |
Beta Was this translation helpful? Give feedback.
-
不是很理解,第三题 pub enum Assert {} 这个枚举中,CHECK是一个const 泛型 |
Beta Was this translation helpful? Give feedback.
-
第一题怎么理解呢?直接用类型调用函数?我从c/c++语言的角度来看,不是要先定义一个变量或者参数,然后传给函数作为形参吗?那这里这样调用函数 reg_fn(S(A)) 是什么意思呢?最终是什么都没干吗?编译器怎么生成代码的这里。 |
Beta Was this translation helpful? Give feedback.
-
第三题的:我们知道 |
Beta Was this translation helpful? Give feedback.
-
🌟🌟 <T, const N: usize> 是结构体类型的一部分,和数组类型一样,这意味着长度不同会导致类型不同: Array<i32, 3> 和 Array<i32, 4> 是不同的类型 第一题:我理解的知识点是:数组每一个元素类型都要一样,如果第一个元素是[i32,3] 第二个是[i32,4] 就没法 在外面使用[]来表达 错误示范:[[i32,3],[i32,4]] |
Beta Was this translation helpful? Give feedback.
-
第三题的限制大小size是怎么算的,比如0u8对应的767?没有强类型语言经验的小白看得很懵 |
Beta Was this translation helpful? Give feedback.
-
看评论区吐槽的第三题貌似和现在看到的不一样啊,是更换题目了吗 |
Beta Was this translation helpful? Give feedback.
-
第三题描述改成取极限大小会好点吗 我做的时候直接用3个1水过去了 |
Beta Was this translation helpful? Give feedback.
-
请教下 Assert<{ core::mem::size_of::() < 768 }>: IsTrue, |
Beta Was this translation helpful? Give feedback.
-
最后一题个人理解:
|
Beta Was this translation helpful? Give feedback.
-
《Const 泛型》这一节练习题的第三个例子似乎过不了编译: pub struct MinSlice<T, const N: usize> {
pub head: [T; N],
pub tail: [T],
}
fn main() {
let slice: &[u8] = b"Hello, world";
let reference: Option<&u8> = slice.get(6);
// 我们知道 `.get` 返回的是 `Some(b' ')`
// 但编译器不知道
assert!(reference.is_some());
let slice: &[u8] = b"Hello, world";
// 当编译构建 MinSlice 时会进行长度检查,也就是在编译期我们就知道它的长度是 12
// 在运行期,一旦 `unwrap` 成功,在 `MinSlice` 的作用域内,就再无需任何检查
let minslice = MinSlice::<u8, 12>::from_slice(slice).unwrap();
let value: u8 = minslice.head[6];
assert_eq!(value, b' ')
} 尝试 error[E0599]: no function or associated item named `from_slice` found for struct `MinSlice` in the current scope
--> src/main.rs:17:40
|
1 | pub struct MinSlice<T, const N: usize> {
| -------------------------------------- function or associated item `from_slice` not found for this struct
...
17 | let minslice = MinSlice::<u8, 12>::from_slice(slice).unwrap();
| ^^^^^^^^^^ function or associated item not found in `MinSlice<u8, 12>`
For more information about this error, try `rustc --explain E0599`.
error: could not compile `b` (bin "b") due to 1 previous error
[Process exited 101] |
Beta Was this translation helpful? Give feedback.
-
mark finished |
Beta Was this translation helpful? Give feedback.
-
第六题mixup里为什么用self程序就可以通过,而用&self则通过不了 |
Beta Was this translation helpful? Give feedback.
-
Mixup was a little technical for me. |
Beta Was this translation helpful? Give feedback.
-
done 但还没理解明白 |
Beta Was this translation helpful? Give feedback.
-
Good one there , |
Beta Was this translation helpful? Give feedback.
-
难度大大增加了,跟不上了 😄 |
Beta Was this translation helpful? Give feedback.
-
day4 完成范型 |
Beta Was this translation helpful? Give feedback.
-
day4 完成范型内容 |
Beta Was this translation helpful? Give feedback.
-
Done ✅ |
Beta Was this translation helpful? Give feedback.
-
generics-traits/const-generics
Learning Rust By Practice, narrowing the gap between beginner and skilled-dev with challenging examples, exercises and projects.
https://zh.practice.rs/generics-traits/const-generics.html
Beta Was this translation helpful? Give feedback.
All reactions