の呟きは 14
- (UPD ) #
cargo clipplyで
enum_variant_namesのバグを踏んだんじゃないかという警告メッセージ群に遭遇・・・ これできないの辛い・・・
#[derive(Debug)]
struct Foo { value :i32 }
impl Foo {
fn next(mut self) -> Self {
self.value += 1;
self
}
}
#[derive(Debug)]
struct Bar { foo: Foo }
impl Bar {
fn do_it(&mut self) {
self.foo = self.foo.next().next().next();
}
}
fn main() {
let mut bar = Bar{foo:Foo{value: 10}};
println!("{:?}", bar);
bar.do_it();
println!("{:?}", bar);
}Copyトレイトを実装しろってエラーメッセージなのだけど
Copyトレイトは違うんだよな・・・まぁこうなるしかないわな・・・
#[derive(Debug,Clone)]
struct Foo { value :i32 }
impl Foo {
fn next(&mut self) -> &mut Self {
self.value += 1;
self
}
}
#[derive(Debug)]
struct Bar { foo: Foo }
impl Bar {
fn do_it(&mut self) {
self.foo = self.foo.next().next().next().clone();
}
}
fn main() {
let mut bar = Bar{foo:Foo{value: 10}};
println!("{:?}", bar);
bar.do_it();
println!("{:?}", bar);
}
inwardly://docs/rust/std/borrow/trait.ToOwned.html#tymethod.to_owned
to_ownedて
Borrowかぶってるやつの中身のコピー取るときに使うメソッドなのか・・・?違うか
Borrowを被せて返すって感じなのか・・・?- (UPD ) #
impl Borrow<Foo> for Bar {...}
なんてのがあったら
let foo:Foo = Foo::new();
で
let bar:Bar = foo.to_owned();
でコピれるってこと?いあ、よくわかんね
use std::borrow::Borrow;
#[derive(Debug,Clone)]
struct Foo { value :i32 }
#[derive(Debug)]
struct Bar { foo: Foo }
impl Borrow<Foo> for Bar {
fn borrow(&self) -> &Foo {
&self.foo
}
}
fn main() {
let bar = Bar{foo:Foo{value: 10}};
println!("{:?}", bar);
let foo: &Foo = bar.borrow();
println!("{:?}", foo);
let foo2: Foo = foo.to_owned();
println!("{:?}", foo2);
}なる、ほどね・・・?
use std::borrow::*;
#[derive(Debug)]
struct Foo { value :i32 }
#[derive(Debug)]
struct Bar { foo: Foo }
impl Borrow<Foo> for Bar {
fn borrow(&self) -> &Foo {
&self.foo
}
}
impl ToOwned for Foo {
type Owned = Bar;
fn to_owned(&self) -> Self::Owned {
Bar { foo: Foo{ value: self.value } }
}
}
fn main() {
let bar = Bar{foo:Foo{value: 10}};
println!("{:?}", bar);
let foo: &Foo = bar.borrow();
println!("{:?}", foo);
let bar2: Bar = foo.to_owned();
println!("{:?}", bar2);
let foo2: &Foo = foo.clone();
println!("{:?}", foo2);
}Fromトレイトだとmoveなのか・・
inwardly://docs/rust/std/convert/trait.From.htmlIntoもmoveだな・・・
inwardly://docs/rust/std/convert/trait.Into.html