basic/trait/generic #709
Replies: 40 comments 69 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.
-
fn largest<T: std::cmp::PartialOrd>(list: &[T]) -> Option<&T> {
if let Some(n) = list.get(0) {
let mut largest = n;
for item in list {
if item > largest {
largest = item;
}
}
Some(largest)
} else {
None
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn largest_i32() {
let list = vec![1, 2, 3, 4, 5];
assert_eq!(largest(&list), Some(&5));
}
#[test]
fn largest_str() {
let list = vec!["1", "2", "3", "4", "5"];
assert_eq!(largest(&list), Some(&"5"));
}
#[test]
fn largest_string() {
let list = vec![
"1".to_string(),
"2".to_string(),
"3".to_string(),
"4".to_string(),
"5".to_string(),
];
assert_eq!(largest(&list), Some(&"5".to_string()));
}
#[test]
fn largest_none() {
let list: Vec<&str> = vec![];
assert_eq!(largest(&list), None);
}
} 这样会不会更好点? |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
// 目前只能在nightly版本下使用
#![allow(incomplete_features)]
#![feature(generic_const_exprs)]
fn something<T>(val: T)
where
Assert<{ core::mem::size_of::<T>() < 768 }>: IsTrue,
// ^-----------------------------^ 这里是一个 const 表达式,换成其它的 const 表达式也可以
{
//
}
fn main() {
something([0u8; 0]); // ok
something([0u8; 512]); // ok
something([0u8; 1024]); // 编译错误,数组长度是1024字节,超过了768字节的参数长度限制
}
// ---
pub enum Assert<const CHECK: bool> {
//
}
pub trait IsTrue {
//
}
impl IsTrue for Assert<true> {
//
} 这个例子一下给我干懵了,
又是啥?怎么突然冒出来个where? |
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.
-
basic/trait/generic
https://course.rs/basic/trait/generic.html
Beta Was this translation helpful? Give feedback.
All reactions