Skip to content

Commit

Permalink
update original
Browse files Browse the repository at this point in the history
  • Loading branch information
funkill committed Nov 27, 2024
1 parent 59eb094 commit f86181e
Show file tree
Hide file tree
Showing 64 changed files with 324 additions and 275 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
extern "C" {
unsafe extern "C" {
fn abs(input: i32) -> i32;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
static HELLO_WORLD: &str = "Hello, world!";
unsafe extern "C" {
safe fn abs(input: i32) -> i32;
}

fn main() {
println!("name is: {HELLO_WORLD}");
println!("Absolute value of -3 according to C: {}", abs(-3));
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
static mut COUNTER: u32 = 0;

/// SAFETY: Calling this from more than a single thread at a time is undefined
/// behavior, so you *must* guarantee you only call it from a single thread at
/// a time.
unsafe fn add_to_count(inc: u32) {
COUNTER += inc;
}
static HELLO_WORLD: &str = "Hello, world!";

fn main() {
unsafe {
// SAFETY: This is only called from a single thread in `main`.
add_to_count(3);
println!("COUNTER: {}", COUNTER);
}
println!("name is: {HELLO_WORLD}");
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
unsafe trait Foo {
// methods go here
}
static mut COUNTER: u32 = 0;

unsafe impl Foo for i32 {
// method implementations go here
/// SAFETY: Calling this from more than a single thread at a time is undefined
/// behavior, so you *must* guarantee you only call it from a single thread at
/// a time.
unsafe fn add_to_count(inc: u32) {
COUNTER += inc;
}

fn main() {}
fn main() {
unsafe {
// SAFETY: This is only called from a single thread in `main`.
add_to_count(3);
println!("COUNTER: {}", COUNTER);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "traits-example"
name = "unsafe-example"
version = "0.1.0"
edition = "2021"

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
unsafe trait Foo {
// methods go here
}

unsafe impl Foo for i32 {
// method implementations go here
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub trait Iterator<T> {
fn next(&mut self) -> Option<T>;
pub trait Iterator {
type Item;

fn next(&mut self) -> Option<Self::Item>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub trait Iterator<T> {
fn next(&mut self) -> Option<T>;
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// ANCHOR: here
trait Pilot {
fn fly(&self);
}
Expand Down Expand Up @@ -25,10 +26,6 @@ impl Human {
println!("*waving arms furiously*");
}
}

// ANCHOR: here
fn main() {
let person = Human;
person.fly();
}
// ANCHOR_END: here

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ impl Human {
// ANCHOR: here
fn main() {
let person = Human;
Pilot::fly(&person);
Wizard::fly(&person);
person.fly();
}
// ANCHOR_END: here
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
$ cargo run
Compiling traits-example v0.1.0 (file:///projects/traits-example)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.54s
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.46s
Running `target/debug/traits-example`
A baby dog is called a Spot
This is your captain speaking.
Up!
*waving arms furiously*
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
trait Animal {
fn baby_name() -> String;
trait Pilot {
fn fly(&self);
}

struct Dog;
trait Wizard {
fn fly(&self);
}

struct Human;

impl Pilot for Human {
fn fly(&self) {
println!("This is your captain speaking.");
}
}

impl Dog {
fn baby_name() -> String {
String::from("Spot")
impl Wizard for Human {
fn fly(&self) {
println!("Up!");
}
}

impl Animal for Dog {
fn baby_name() -> String {
String::from("puppy")
impl Human {
fn fly(&self) {
println!("*waving arms furiously*");
}
}

// ANCHOR: here
fn main() {
println!("A baby dog is called a {}", Dog::baby_name());
let person = Human;
Pilot::fly(&person);
Wizard::fly(&person);
person.fly();
}
// ANCHOR_END: here
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
$ cargo run
Compiling traits-example v0.1.0 (file:///projects/traits-example)
error[E0790]: cannot call associated function on trait without specifying the corresponding `impl` type
--> src/main.rs:20:43
|
2 | fn baby_name() -> String;
| ------------------------- `Animal::baby_name` defined here
...
20 | println!("A baby dog is called a {}", Animal::baby_name());
| ^^^^^^^^^^^^^^^^^^^ cannot call associated function of trait
|
help: use the fully-qualified path to the only available implementation
|
20 | println!("A baby dog is called a {}", <Dog as Animal>::baby_name());
| +++++++ +

For more information about this error, try `rustc --explain E0790`.
error: could not compile `traits-example` (bin "traits-example") due to 1 previous error
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.54s
Running `target/debug/traits-example`
A baby dog is called a Spot
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ impl Animal for Dog {
}
}

// ANCHOR: here
fn main() {
println!("A baby dog is called a {}", Animal::baby_name());
println!("A baby dog is called a {}", Dog::baby_name());
}
// ANCHOR_END: here
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
$ cargo run
Compiling traits-example v0.1.0 (file:///projects/traits-example)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.48s
Running `target/debug/traits-example`
A baby dog is called a puppy
error[E0790]: cannot call associated function on trait without specifying the corresponding `impl` type
--> src/main.rs:20:43
|
2 | fn baby_name() -> String;
| ------------------------- `Animal::baby_name` defined here
...
20 | println!("A baby dog is called a {}", Animal::baby_name());
| ^^^^^^^^^^^^^^^^^^^ cannot call associated function of trait
|
help: use the fully-qualified path to the only available implementation
|
20 | println!("A baby dog is called a {}", <Dog as Animal>::baby_name());
| +++++++ +

For more information about this error, try `rustc --explain E0790`.
error: could not compile `traits-example` (bin "traits-example") due to 1 previous error
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ impl Animal for Dog {

// ANCHOR: here
fn main() {
println!("A baby dog is called a {}", <Dog as Animal>::baby_name());
println!("A baby dog is called a {}", Animal::baby_name());
}
// ANCHOR_END: here
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
$ cargo run
Compiling traits-example v0.1.0 (file:///projects/traits-example)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.46s
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.48s
Running `target/debug/traits-example`
This is your captain speaking.
Up!
*waving arms furiously*
A baby dog is called a puppy
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
// ANCHOR: here
use std::fmt;
trait Animal {
fn baby_name() -> String;
}

struct Dog;

trait OutlinePrint: fmt::Display {
fn outline_print(&self) {
let output = self.to_string();
let len = output.len();
println!("{}", "*".repeat(len + 4));
println!("*{}*", " ".repeat(len + 2));
println!("* {output} *");
println!("*{}*", " ".repeat(len + 2));
println!("{}", "*".repeat(len + 4));
impl Dog {
fn baby_name() -> String {
String::from("Spot")
}
}

impl Animal for Dog {
fn baby_name() -> String {
String::from("puppy")
}
}
// ANCHOR_END: here

fn main() {}
// ANCHOR: here
fn main() {
println!("A baby dog is called a {}", <Dog as Animal>::baby_name());
}
// ANCHOR_END: here
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
// ANCHOR: here
use std::fmt;

struct Wrapper(Vec<String>);

impl fmt::Display for Wrapper {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[{}]", self.0.join(", "))
trait OutlinePrint: fmt::Display {
fn outline_print(&self) {
let output = self.to_string();
let len = output.len();
println!("{}", "*".repeat(len + 4));
println!("*{}*", " ".repeat(len + 2));
println!("* {output} *");
println!("*{}*", " ".repeat(len + 2));
println!("{}", "*".repeat(len + 4));
}
}
// ANCHOR_END: here

fn main() {
let w = Wrapper(vec![String::from("hello"), String::from("world")]);
println!("w = {w}");
}
fn main() {}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "types-example"
name = "traits-example"
version = "0.1.0"
edition = "2021"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
fn main() {
// ANCHOR: here
let f: Box<dyn Fn() + Send + 'static> = Box::new(|| println!("hi"));
use std::fmt;

fn takes_long_type(f: Box<dyn Fn() + Send + 'static>) {
// --snip--
}
struct Wrapper(Vec<String>);

fn returns_long_type() -> Box<dyn Fn() + Send + 'static> {
// --snip--
// ANCHOR_END: here
Box::new(|| ())
// ANCHOR: here
impl fmt::Display for Wrapper {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[{}]", self.0.join(", "))
}
// ANCHOR_END: here
}

fn main() {
let w = Wrapper(vec![String::from("hello"), String::from("world")]);
println!("w = {w}");
}
Loading

0 comments on commit f86181e

Please sign in to comment.