The use declaration

The use declaration can be used to bind a full path to a new name, for easier
access.

  1. // Bind the `deeply::nested::function` path to `other_function`.
  2. use deeply::nested::function as other_function;
  3. fn function() {
  4. println!("called `function()`");
  5. }
  6. mod deeply {
  7. pub mod nested {
  8. pub fn function() {
  9. println!("called `deeply::nested::function()`");
  10. }
  11. }
  12. }
  13. fn main() {
  14. // Easier access to `deeply::nested::function`
  15. other_function();
  16. println!("Entering block");
  17. {
  18. // This is equivalent to `use deeply::nested::function as function`.
  19. // This `function()` will shadow the outer one.
  20. use deeply::nested::function;
  21. function();
  22. // `use` bindings have a local scope. In this case, the
  23. // shadowing of `function()` is only in this block.
  24. println!("Leaving block");
  25. }
  26. function();
  27. }