- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为一个 API 建模,其中方法重载非常适合。我天真的尝试失败了:
// fn attempt_1(_x: i32) {}
// fn attempt_1(_x: f32) {}
// Error: duplicate definition of value `attempt_1`
然后我添加了一个枚举并完成了:
enum IntOrFloat {
Int(i32),
Float(f32),
}
fn attempt_2(_x: IntOrFloat) {}
fn main() {
let i: i32 = 1;
let f: f32 = 3.0;
// Can't pass the value directly
// attempt_2(i);
// attempt_2(f);
// Error: mismatched types: expected enum `IntOrFloat`
attempt_2(IntOrFloat::Int(i));
attempt_2(IntOrFloat::Float(f));
// Ugly that the caller has to explicitly wrap the parameter
}
通过快速搜索,我找到了 found some references谈论重载,所有这些似乎都以“我们不允许这样做,但尝试一下特性”结尾。所以我尝试了:
enum IntOrFloat {
Int(i32),
Float(f32),
}
trait IntOrFloatTrait {
fn to_int_or_float(&self) -> IntOrFloat;
}
impl IntOrFloatTrait for i32 {
fn to_int_or_float(&self) -> IntOrFloat {
IntOrFloat::Int(*self)
}
}
impl IntOrFloatTrait for f32 {
fn to_int_or_float(&self) -> IntOrFloat {
IntOrFloat::Float(*self)
}
}
fn attempt_3(_x: &dyn IntOrFloatTrait) {}
fn main() {
let i: i32 = 1;
let f: f32 = 3.0;
attempt_3(&i);
attempt_3(&f);
// Better, but the caller still has to explicitly take the reference
}
这是我最接近方法重载的方法吗?有更清洁的方法吗?
最佳答案
是的,有,你几乎已经明白了。特征是要走的路,但你不需要特征对象,使用泛型:
#[derive(Debug)]
enum IntOrFloat {
Int(i32),
Float(f32),
}
trait IntOrFloatTrait {
fn to_int_or_float(&self) -> IntOrFloat;
}
impl IntOrFloatTrait for i32 {
fn to_int_or_float(&self) -> IntOrFloat {
IntOrFloat::Int(*self)
}
}
impl IntOrFloatTrait for f32 {
fn to_int_or_float(&self) -> IntOrFloat {
IntOrFloat::Float(*self)
}
}
fn attempt_4<T: IntOrFloatTrait>(x: T) {
let v = x.to_int_or_float();
println!("{:?}", v);
}
fn main() {
let i: i32 = 1;
let f: f32 = 3.0;
attempt_4(i);
attempt_4(f);
}
看到它工作 here .
关于overloading - 我怎样才能近似方法重载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25265527/
所以我必须用以下方法来近似 Pi:4*(1-1/3+1/5-1/7+1/9-...)。它也应该基于迭代次数。所以函数应该是这样的: >>> piApprox(1) 4.0 >>> piApprox(1
输入:图 G 输出:多个独立集,使得一个节点对所有独立集的成员资格是唯一的。因此,节点与它自己的集合中的任何节点都没有连接。这是一个示例路径。 由于这里需要澄清,因此再次改写: 将给定的图划分为多个集
我已经使用查找表和低阶多项式近似实现了定点 log2 函数,但对整个 32 位定点范围 [-1,+1) 的准确度不太满意。输入格式为 s0.31,输出格式为 s15.16。 我在这里发布这个问题,以便
大多数拥有CS学位的人当然会知道Big O stands for是什么。 它可以帮助我们评估算法的可扩展性。 但是我很好奇,您如何计算或估算算法的复杂性? 最佳答案 我会尽力在这里简单地解释它,但要注
我的目标是近似二项式变量总和的分布。我使用以下纸张The Distribution of a Sum of Binomial Random Variables作者:肯·巴特勒和迈克尔·斯蒂芬斯。 我想
我知道有方法 approximate cubic Bezier curves ( this page 也是一个很好的引用),但是有没有更快的方法来逼近 N 次贝塞尔曲线?还是只能使用下面的概括? 来自
大多数拥有CS学位的人当然会知道Big O stands for是什么。 它有助于我们评估算法的可扩展性。 但是我很好奇,您如何计算或估算算法的复杂性? 最佳答案 我会尽力在这里简单地解释它,但要注意
我是 C++ 和编码本身的初学者,所以请原谅任何词汇错误。我找不到这个具体问题,但在互联网上找到了类似的问题,但我仍然很难获得我需要的结果。 所以我使用莱布尼茨公式来近似 pi,即: pi = 4 ·
有多种方法可以通过显示名称查找联系人。例如这个答案Android - Find a contact by display name 但是我需要找到模糊匹配的联系人。例如如果找不到“Kim”,我需要返回
我一直在尝试使用以下代码使用级数表示来近似 e 以获得尽可能多的精度数字,但无论我计算多少项,精度数字的数量似乎都保持不变。即: 2.718281984329223632812500000000000
大多数拥有CS学位的人当然会知道Big O stands for是什么。 它可以帮助我们评估算法的可扩展性。 但是我很好奇,您如何计算或估算算法的复杂性? 最佳答案 我会尽力在这里简单地解释它,但要注
大多数拥有CS学位的人当然会知道Big O stands for是什么。 它可以帮助我们评估算法的可扩展性。 但是我很好奇,您如何计算或估算算法的复杂性? 最佳答案 我会尽力在这里简单地解释它,但要注
大多数拥有计算机科学学位的人肯定知道什么是Big O stands for。 它有助于我们衡量一个算法的实际效率,如果您知道在what category the problem you are try
大多数拥有计算机科学学位的人肯定知道什么是Big O stands for。 它有助于我们衡量一个算法的实际效率,如果您知道在what category the problem you are try
我做了很多随机的数学程序来帮助我完成作业(合成除法是最有趣的),现在我想反转一个激进的表达式。 例如,在我方便的 TI 计算器中我得到 .2360679775 好吧,我想将该数字转换为等效的无理数表达
我可以通过 CPU 分析器看到,compute_variances() 是我项目的瓶颈。 % cumulative self self total
大多数拥有 CS 学位的人肯定知道什么 Big O stands for . 它帮助我们衡量算法的可扩展性。 但我很好奇,你如何计算或近似算法的复杂性? 最佳答案 我会尽我所能用简单的术语在这里解释它
这是迄今为止我的代码, from math import * def main(): sides = eval(input("Enter the number of sides:"))
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
大多数拥有 CS 学位的人肯定知道什么 Big O stands for . 它帮助我们衡量算法的扩展性。 但我很好奇,你如何计算或近似算法的复杂性? 最佳答案 我会尽我所能用简单的术语在这里解释它,
我是一名优秀的程序员,十分优秀!