Skip to content

Confusing error mesage #52284

Closed
Closed
@mmrath

Description

@mmrath

Source

    fn main() {
        println!("Hello world");
        let arr1 = [10, 15, 20, 25];
        let arr2 = [1, 5, 9, 13, 17, 21, 25];
        let med = median(&arr1, &arr2);
        println!("{:?}", med);
    }

    fn median(arr1: &[i32], arr2: &[i32]) -> Result<f64, String> {
        use std::cmp;
        if arr1.len() == 0 && arr2.len() == 0 {
            Err("No elements".to_owned())
        }
        let x = arr1.len();
        let y = arr2.len();

        if x > y {
            return median(arr2, arr1);
        }

        let mut low: usize = 0;
        let mut high = x;
        while low <= high {
            let part_x = (low + high) / 2;
            let part_y = (x + y + 1) / 2 - part_x;

            let max_x = if part_x == 0 {
                i32::min_value()
            } else {
                arr1[part_x - 1]
            };
            let min_x = if part_x == x {
                i32::max_value()
            } else {
                arr1[part_x]
            };

            let max_y = if part_y == 0 {
                i32::min_value()
            } else {
                arr2[part_y - 1]
            };
            let min_y = if part_y == y {
                i32::max_value()
            } else {
                arr2[part_y]
            };

            if max_x <= min_y && max_y <= min_x {
                if (x + y) % 2 == 0 {
                    return Ok((cmp::max(max_x, max_y) + cmp::min(min_x, min_y)) as f64 / 2.0);
                } else {
                    return Ok(cmp::max(max_x, max_y) as f64);
                }
            } else if max_x > min_y {
                high = part_x - 1;
            } else {
                low = part_x + 1;
            }
        }
        Err("Median not found".to_owned())
    }]]

Link to playground: https://play.rust-lang.org/?gist=4f80f043a2f29bf2810ecc8f75575f8d&version=stable&mode=debug&edition=2015

Current error message:

error[E0308]: mismatched types
  --> src/main.rs:12:9
   |
12 |         Err("No elements".to_owned())
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- help: try adding a semicolon: `;`
   |         |
   |         expected (), found enum `std::result::Result`
   |
   = note: expected type `()`
			  found type `std::result::Result<_, std::string::String>`

Expected

The compiler should probably tell me that I am missing a return statement.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions