From 7374f65411d76dfe855d345f87dddb2fcda7ce5e Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Fri, 10 Jun 2016 00:30:58 -0400 Subject: Unsafe pointer example. --- rust/graph/Cargo.toml | 4 ++++ rust/graph/graph2.rs | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 rust/graph/graph2.rs (limited to 'rust/graph') diff --git a/rust/graph/Cargo.toml b/rust/graph/Cargo.toml index adabedc..eb0841b 100644 --- a/rust/graph/Cargo.toml +++ b/rust/graph/Cargo.toml @@ -9,3 +9,7 @@ itertools = "0.4.15" [[bin]] name = "graph" path = "graph.rs" + +[[bin]] +name = "graph2" +path = "graph2.rs" diff --git a/rust/graph/graph2.rs b/rust/graph/graph2.rs new file mode 100644 index 0000000..1e57f19 --- /dev/null +++ b/rust/graph/graph2.rs @@ -0,0 +1,17 @@ +#[derive(Debug)] +struct Node<'a> { + label: i32, + next: Option<&'a Node<'a>>, +} + + +fn main () { + let mut n = Node { label: 1, next: None }; + unsafe { + let raw = &mut n as *mut Node; + (*raw).next = Some(&n); + } + println!("{}", n.label); + println!("{}", n.next.unwrap().label); + println!("{}", n.next.unwrap().next.unwrap().label); +} -- cgit v1.2.3