basic/trait/generic #709
Replies: 44 comments 72 replies
-
struct Point<T, U> { impl<T, U> Point<T, U> { fn main() {
} |
Beta Was this translation helpful? Give feedback.
-
struct Point<T, U> { impl<T, U> Point<T, U> { fn main() {
} 为什么self改为&self不可以 |
Beta Was this translation helpful? Give feedback.
-
我为啥觉得为了实现泛型 add 添加的 |
Beta Was this translation helpful? Give feedback.
-
Assert<{ core::mem::size_of::() < 768 }>: IsTrue, 这个 const泛型表达式,好丑,好难理解 |
Beta Was this translation helpful? Give feedback.
-
可与 TypeScript 的泛型一战否 🐶 |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
吊打java泛型擦除 |
Beta Was this translation helpful? Give feedback.
-
largest方法跑不过 let mut largest = arr[0];
|
Beta Was this translation helpful? Give feedback.
-
fn display_array<T: std::fmt::Debug, const N: usize>(arr: [T; N]) {
println!("{}",N)
} |
Beta Was this translation helpful? Give feedback.
-
struct Point { impl Point { fn main() {
请问一下在x方法中,为什么最后需要返回&T类型呢?本地尝试了取消引用,提示cannot move out of |
Beta Was this translation helpful? Give feedback.
-
可以直接运行的largest方法, 加入 fn largest<T: PartialOrd + Copy>(list: &[T]) -> T {
let mut largest = list[0];
for &item in list.iter() {
{
if item > largest {
largest = item;
}
}
}
largest
} |
Beta Was this translation helpful? Give feedback.
-
Assert<{ core::mem::size_of::<T>() < 768 }>: IsTrue 这个真的是我现在就能看的代码吗? 上一步还是1+1=2,下一步就要解二元一次方程😂 |
Beta Was this translation helpful? Give feedback.
-
个人觉得、 习题还需要优化一下 |
Beta Was this translation helpful? Give feedback.
-
刚从 |
Beta Was this translation helpful? Give feedback.
-
fn largest<T: std::cmp::PartialOrd>(list: &[T]) -> T {
} 这段代码在最新的rust 1.64版本上依然无法编译通过,请高手指点下呢,谢谢 |
Beta Was this translation helpful? Give feedback.
-
let p2 = Point { x: "Hello", y: 'c'}; 为什么我们定义结构体不允许字段类型为 &str,编译器提示需要生命周期标注。但泛型这里就可以 |
Beta Was this translation helpful? Give feedback.
-
const fn 催更 |
Beta Was this translation helpful? Give feedback.
-
果然,牵涉到泛型,所有的语言都一样的丑陋,令人困惑的不一致的语法。 |
Beta Was this translation helpful? Give feedback.
-
fn largest<T: PartialOrd + Copy>(list: &[T]) -> &T {
let mut largest = &list[0];
for item in list.iter() {
if item > largest {
largest = &item;
}
}
largest
} 为什么这段代码可以编译通过, largest = &item; item作为临时变量,下一次遍历会被销毁,它的引用怎么还能帮到的到largest |
Beta Was this translation helpful? Give feedback.
-
电脑里有一个付费才能坐的斯拉夫大牢 |
Beta Was this translation helpful? Give feedback.
-
看完泛型,再显现之前编译器的各种检查,不得不说RUST的编译器终究承受了太多。 |
Beta Was this translation helpful? Give feedback.
-
其实这样也行,哈哈哈,可以不用const泛型: fn display_array<T: std::fmt::Debug>(arr: T) {
println!("{:?}", arr);
}
fn main() {
let arr: [i32; 3] = [1, 2, 3];
display_array(arr);
let arr: [i32; 2] = [1, 2];
display_array(arr);
} |
Beta Was this translation helpful? Give feedback.
-
|
|
Beta Was this translation helpful? Give feedback.
-
这三个结构体是类单元结构体(unit-like structs)吗? 为什么还有 |
Beta Was this translation helpful? Give feedback.
-
有点迷糊这里在《Const 泛型》习题中,实例2给出了const泛型参数使用的三个条件:
fn foo<const N: usize>() {}
fn bar<T, const M: usize>() {
foo::<M>(); // ok: 符合第一种
foo::<2021>(); // ok: 符合第二种
foo::<{20 * 100 + 20 * 10 + 1}>(); // ok: 符合第三种
foo::<{ M + 1 }>(); // error: 违背第三种,const 表达式中不能有泛型参数 M
foo::<{ std::mem::size_of::<T>() }>(); // error: 泛型表达式包含了泛型参数 T
let _: [u8; M]; // ok: 符合第一种
let _: [u8; std::mem::size_of::<T>()]; // error: 泛型表达式包含了泛型参数 T
}
fn main() {} 问题就来了:代码中 |
Beta Was this translation helpful? Give feedback.
-
let buffer = Buffer:: { |
Beta Was this translation helpful? Give feedback.
-
泛型详解中的列子,加上比较运算属性也无法编译通过呀?我按照错误提示修改了下
这样可以通过编译 |
Beta Was this translation helpful? Give feedback.
-
fn largest<T>(list: &[T]) -> T {
let mut largest = list[0];
for &item in list.iter() {
if item > largest {
largest = item;
}
}
largest
} 为啥for循环时用 &item , if判断的时候用的 item , &item 是对 |
Beta Was this translation helpful? Give feedback.
-
提个小建议,如果正文中能直接加上某些专有名词对应的英文就更好了,比如这一章中的“多态”,以后看英文资料的时候就能直接对应上了。 |
Beta Was this translation helpful? Give feedback.
-
basic/trait/generic
https://course.rs/basic/trait/generic.html
Beta Was this translation helpful? Give feedback.
All reactions