Skip to content

Type alias do not used in functions #76606

Closed
@juev

Description

@juev

First, create new lib with type alias and function:

pub type OperationType = String;

lazy_static! {
    pub static ref OPERATION_TYPE_BUY: OperationType = String::from("Buy");
}

    pub fn limit_order(
        &self,
        account_id: &str,
        figi: &str,
        lots: i64,
        operation: OperationType,
        price: f64,
    ) -> Result<PlacedOrder> {
    ...
    }

Then, we use this crate in our program:

    let limit_order =
         rest_client.limit_order("", "BBG000BB88K4", 2, OPERATION_TYPE_BUY, 11 as f64)?;

OPERATION_TYPE_BUY has type OperationType, this type we expect in limit_order function, but we have error:

error[E0308]: mismatched types
  --> src/main.rs:38:57
   |
38 |          rest_client.limit_order("", "BBG000BB88K4", 2, OPERATION_TYPE_BUY, 11 as f64)?;
   |                                                         ^^^^^^^^^^^^^^^^^^
   |                                                         |
   |                                                         expected struct `std::string::String`, found struct `invest_openapi_rs_sdk::OPERATION_TYPE_BUY`
   |                                                         help: try using a conversion method: `OPERATION_TYPE_BUY.to_string()`

error: aborting due to previous error

If our function expect OperationType type, why rust asked me convert to String?

Meta

rustc --version --verbose:

$ rustc --version --verbose
rustc 1.46.0 (04488afe3 2020-08-24)
binary: rustc
commit-hash: 04488afe34512aa4c33566eb16d8c912a3ae04f9
commit-date: 2020-08-24
host: x86_64-apple-darwin
release: 1.46.0
LLVM version: 10.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions