Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I need to use self::core::ops?

Tags:

rust

I'm trying to use Mul from core.

This is suggested by the compiler and works:

extern crate core;

use self::core::ops::Mul;

but why doesn't

extern crate core;

use core::ops::Mul;

work?

I get the error error: unresolved import `core::ops::Mul`. Did you mean `self::core::ops`?

like image 586
yong Avatar asked Jun 15 '15 11:06

yong


People also ask

What happens if you don't use self in Python?

If there was no self argument, the same class couldn't hold the information for both these objects. However, since the class is just a blueprint, self allows access to the attributes and methods of each object in python. This allows each object to have its own attributes and methods.

What does return self do in Python?

return self would return the object that the method was called from.

What is def __ init __( self in Python?

__init__ is one of the reserved methods in Python. In object oriented programming, it is known as a constructor. The __init__ method can be called when an object is created from the class, and access is required to initialize the attributes of the class.

What is the purpose of the __ Init__ method in Python?

The __init__ method lets the class initialize the object's attributes and serves no other purpose. It is only used within classes.


1 Answers

An extern crate x; loads x into the current namespace. use statements are absolute paths unless they start with self::, so if you put your extern crate core; anywhere but the crate root then you need to specify an absolute path or use self::.

mod foo {
    mod bar {
        extern crate core;
        use foo::bar::core::ops::Mul;
        // or `use self::core::ops::Mul;`
        // or even `use super::bar::core::ops::Mul;` if you’re mad
        // but not `use core::ops::Mul;`.
    }
}

… but as a general rule you shouldn’t use core directly anyway. All the stable stuff from it is available in std which is included automatically.

like image 123
Chris Morgan Avatar answered Oct 09 '22 03:10

Chris Morgan