generics-traits/const-generics #208
Replies: 53 comments 29 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.
-
done1 |
Beta Was this translation helpful? Give feedback.
-
Done. |
Beta Was this translation helpful? Give feedback.
-
DONE |
Beta Was this translation helpful? Give feedback.
-
mark |
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.
-
done |
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.
-
并不简单! |
Beta Was this translation helpful? Give feedback.
-
这个第7题。我怎么写都报错。我打开答案一看,我真的想笑.... struct Point<T> {
x: T,
y: T,
}
impl<T> Point<T>
where
T: std::ops::Add<Output = T>,
T: std::ops::Mul<Output = T>,
T: Copy,
T: Into<f32>,
{
fn distance_from_origin(&self) -> f32 {
let x = self.x.into();
let y = self.y.into();
(x.powi(2) + y.powi(2)).sqrt()
}
}
fn main() {
let p = Point { x: 5, y: 10 };
println!("{}", p.distance_from_origin());
} 我觉得我这个答案更好,只是会报错
查了下, struct Point<T> {
x: T,
y: T,
}
impl <T>Point<T>
where
T: std::ops::Add<Output = T>,
T: std::ops::Mul<Output = T>,
T: Copy,
T: Into<f64>,
{
fn distance_from_origin(&self) -> f64 {
let x = self.x.into();
let y = self.y.into();
(x.powi(2) + y.powi(2)).sqrt()
}
}
fn main() {
let p = Point{x: 5, y: 10};
println!("{}",p.distance_from_origin())
}
人在无语的时候真的会笑,当看到参考答案的时候🙂 |
Beta Was this translation helpful? Give feedback.
-
关于const generic部分的第三题有个问题。
查了一下原因,发现是题干里使用的#![feature(generic_const_exprs)]特性已经在最新的(目前是1.84.1)稳定版中被移除了. 对于各位贴代码到本地ide中运行的同学,有两个方案可以解决这个问题:
我猜这里应该是想综合考察前面关于类型大小的知识点?不知道作者大大是怎么考虑的。我稍后会提交一个pr,针对当前1.84版本做一个修复,请参考。 |
Beta Was this translation helpful? Give feedback.
-
done. |
Beta Was this translation helpful? Give feedback.
-
第六题:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
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