如何制作具有特征的泛型?

时间:2019-04-04 15:26:33

标签: rust

pub struct Triangle<T: Float + std::clone::Clone, V: vector::Vector<T>> {
    point1: V,
    point2: V,
    point3: V,
}

由于未使用T(因此,稍后在方法中使用T),因此无法编译此代码块

我已经尝试过这种语法

pub struct Triangle<V: vector::Vector<T: Float + std::clone::Clone>> {
    point1: V,
    point2: V,
    point3: V,
}

错误:

expected one of `!`, `(`, `+`, `,`, `::`, `<`, or `>`, found `:`

expected one of 7 possible tokens here

和此语法

pub struct Triangle2<V> where V: vector::Vector<T> where T: Float {
    point1: V,
    point2: V,
    point3: V,
}

错误:

expected `where`, or `{` after struct name, found keyword `where`

expected `where`, or `{` after struct name

那行不通。

他们是解决此问题的一种方法

1 个答案:

答案 0 :(得分:4)

我认为您的类型myaccount/view-order.php看起来或多或少像这样。

<p><?php
    /* translators: 1: order number 2: order date 3: order status */
    printf(
        __( 'Order #%1$s was placed on %2$s and is currently %3$s.', 'woocommerce' ),
        '<mark class="order-number">' . $order->get_order_number() . '</mark>',
        '<mark class="order-date">' . wc_format_datetime( $order->get_date_created() ) . '</mark>',
        '<mark class="order-status">' . wc_get_order_status_name( $order->get_status() ) . '</mark>'
    );
?></p>

解决方案是声明多个泛型类型并分别列出其类型约束:类型Vector必须实现pub trait Vector<T> { // Some functions } ,类型V反过来必须实现Vector<T>和{ {1}}。

T

我正在使用std::marker::PhantomData保存否则未使用的类型信息。

Link to full working code

相关问题