Categories
Programming

Is the rust compiler racist?1 min read

What are you talking about man? How can a compiler be racist? Well, be ready to be surprised. I am going to prove that the rust compiler is racist and racist particularly against Indians.

Proof

I recently applied for a job as a rust developer and a coding challenge came along with it. I sent my resume along with link of the completed challenge. I was shocked by the response that I got back.

We can’t go ahead with your application. We haven’t looked at the coding challenge that you have completed or other work that you have listed in the resume. The rust compiler won’t compile the code unless the person who wrote the code has a college degree.

I was a bit surprised but realised the truth after going through the compiler code

match coder.country {
    Country::India => match coder.degree {
        Degree::College => {
            continue_compilation();
        }
        _ => {
            reject_compilation();
        }
    },
    _ => continue_compilation()
};

Well there it is. #shameOnRustCompiler

Categories
Programming

RUST: non-exhaustive patterns: `&_` not covered pattern `&_` not covered21 sec read

Tried something like this

let a = "hello";

match a{
    "hello"=>{println!("hello")}
}

Error

non-exhaustive patterns: `&_` not covered pattern `&_` not covered

What I thought was wrong

I thought it meant you cannot match references

What the real error was

Just had to add all possibilities in match statement

let a = "hello";

match a {
    "hello" => println!("helloooooo"),
    _ => println!("blahh blahhh"),
}

 

Categories
Programming

Rust Actix web panic when using custom regex1 min read

Using Actix-web v0.7.4

Error – thread ‘arbiter:6461f87e-20ed-4848-9feb-dbe68ab5ea7e:actor’ panicked at ‘index out of bounds: the len is 1 but the index is 1’, /checkout/src/libcore/slice/mod.rs:2079:10

How I corrected it

The loop was accessing an index which didn’t exist. I don’t really know how actix web works or even how Rust works.

pub fn match_with_params  in router.rs in actix-web src was the culprit

So I changed the code to this

    /// Are the given path and parameters a match against this resource?
    pub fn match_with_params(&self, req: &Request, plen: usize) -> Option<Params> {
        let path = &req.path()[plen..];

        match self.tp {
            PatternType::Static(ref s) => if s != path {
                None
            } else {
                Some(Params::with_url(req.url()))
            },
            PatternType::Dynamic(ref re, ref names, _) => {
                if let Some(captures) = re.captures(path) {
                    let mut params = Params::with_url(req.url());
                    let mut idx = 0;
                    let mut passed = false;

                    for capture in captures.iter() {
                        if let Some(ref m) = capture {
                            if !passed {
                                passed = true;
                                continue;
                            }

                            if idx > names.len() - 1 {
                                break;
                            }

                            params.add(
                                names[idx].clone(),
                                ParamItem::UrlSegment(
                                    (plen + m.start()) as u16,
                                    (plen + m.end()) as u16,
                                ),
                            );
                            idx += 1;
                        }
                    }
                    params.set_tail(req.path().len() as u16);
                    Some(params)
                } else {
                    None
                }
            }
            PatternType::Prefix(ref s) => if !path.starts_with(s) {
                None
            } else {
                Some(Params::with_url(req.url()))
            },
        }
    }