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`?
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.
return self would return the object that the method was called from.
__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.
The __init__ method lets the class initialize the object's attributes and serves no other purpose. It is only used within classes.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With