Search
 
SCRIPT & CODE EXAMPLE
 

RUST

armanriazi•rust•concept•coherence

```
use aggregator::{Summary, Tweet};

fn main() {
    let tweet = Tweet {
        username: String::from("horse_ebooks"),
        content: String::from(
            "of course, as you probably already know, people",
        ),
        reply: false,
        retweet: false,
    };

    println!("1 new tweet: {}", tweet.summarize());
}
```
This code prints 1 new tweet: horse_ebooks: of course, as you probably already know, people.

Other crates that depend on the aggregator crate can also bring the Summary trait into scope to implement Summary on their own types. One restriction to note is that we can implement a trait on a type only if at least one of the trait or the type is local to our crate. For example, we can implement standard library traits like Display on a custom type like Tweet as part of our aggregator crate functionality, because the type Tweet is local to our aggregator crate. We can also implement Summary on Vec<T> in our aggregator crate, because the trait Summary is local to our aggregator crate.

But we can’t implement external traits on external types. For example, we can’t implement the Display trait on Vec<T> within our aggregator crate, because Display and Vec<T> are both defined in the standard library and aren’t local to our aggregator crate. This restriction is part of a property called coherence, and more specifically the orphan rule, so named because the parent type is not present. This rule ensures that other people’s code can’t break your code and vice versa. Without the rule, two crates could implement the same trait for the same type, and Rust wouldn’t know which implementation to use.

{
Two trait implementations overlap when there is a non-empty intersection of the traits the implementation is for, the implementations can be instantiated with the same type.
Orphan rules
Given impl<P1..=Pn> Trait<T1..=Tn> for T0, an impl is valid only if at least one of the following is true:

Trait is a local trait
All of
At least one of the types T0..=Tn must be a local type. Let Ti be the first such type.
No uncovered type parameters P1..=Pn may appear in T0..Ti (excluding Ti)
Only the appearance of uncovered type parameters is restricted. Note that for the purposes of coherence, fundamental types are special. The T in Box<T> is not considered covered, and Box<LocalType> is considered local.
}
Comment

PREVIOUS NEXT
Code Example
Rust :: armanriazi•rust•trait•where 
Rust :: initializing array rust 
Rust :: rust random float between 0 and 1 
Lua :: print table lua 
Lua :: roblox how to make rainbow part 
Lua :: if string contains lua 
Lua :: how to delete a key in a table lua 
Lua :: lua for each in table 
Lua :: roblox what is the difference between __index and __newindex 
Lua :: roblox how to tween part color 
Lua :: my second long scripting 
Lua :: luau how to find something in table 
Lua :: prompt game pass purchase 
Lua :: what is the point of local varaibles in lua 
Lua :: how to delete parts with a script in roblox studio 
Lua :: check lua version 
Lua :: get index of value in table lua 
Lua :: how do i do a wait lin lua replit 
Lua :: local in script lua local 
Lua :: how do i use the enums module lua assist 
Lua :: pico8 poke 
Lua :: lua table of all characters 
Matlab :: matlab delete file 
Matlab :: scilab plot 2d function 
Basic :: cmd cd not working 
Basic :: dos assign command output to variable (when output is of multiple lines) 
Elixir :: phoenix system get env 
Elixir :: elixir pipeline 
Scala :: add method to string class scala 
Actionscript :: mount_osxfuse: /Users/em/mount_dev: Input/output error 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =