«前の日記(2017-07-22) 最新 次の日記(2017-07-24)» 編集

ToDo:

  • 99 深呼吸して、おちけつ (~12/31 あと251日)
  • 98 毎日アウトプットを重きにおいた目標をたてる (~12/31 あと251日)
  • 97 買物は行動を変えるもの・ことに限る (~12/31 あと251日)
  • 96 合理的な行動はときに我慢を強いる(~12/31 あと251日)

ɥozɐʞıɥ


2017-07-23 Rustを続けてみて [長年日記]

_ [Rust] トレイト

これも理解しておくのです。

構造体にトレイト*1を追加してあげないとコンパイルエラーになっちゃう。

struct Color{

r: u32,

g: u32,

b: u32,

}

fn main() {

let x = Color{r :255, g: 255, b: 255};

let y = x;

println!("{:?}",y);

}

これだと

error[E0277]: the trait bound `Color: std::fmt::Debug` is not satisfied

--> src/main.rs:11:21

|

11 | println!("{:?}",y);

| ^ the trait `std::fmt::Debug` is not implemented for `Color`

|

= note: `Color` cannot be formatted using `:?`; if it is defined in your crate, add `#[derive(Debug)]` or manually implement it

= note: required by `std::fmt::Debug::fmt`

error: aborting due to previous error

だそうなのでdebug traitを追加

#[derive(Debug)]

struct Color{

r: u32,

g: u32,

b: u32,

}

fn main() {

let x = Color{r :255, g: 255, b: 255};

let y = x;

println!("{:?}",y);

}

この時点ではCopy Traitを追加してないので

#[derive(Debug)]

struct Color{

r: u32,

g: u32,

b: u32,

}

fn main() {

let x = Color{r :255, g: 255, b: 255};

let y = x;

println!("{:?}",y);

println!("{:?}",x);

}

だとコンパイルエラーです

error[E0382]: use of moved value: `x`

--> src/main.rs:12:21

|

10 | let y = x;

| - value moved here

11 | println!("{:?}",y);

12 | println!("{:?}",x);

| ^ value used here after move

|

= note: move occurs because `x` has type `Color`, which does not implement the `Copy` trait

error: aborting due to previous error

そこでCopyとClone Traitを追加してあげると通ります

#[derive(Debug,Copy,Clone)]

struct Color{

r: u32,

g: u32,

b: u32,

}

fn main() {

let x = Color{r :255, g: 255, b: 255};

let y = x;

println!("{:?}",y);

println!("{:?}",x);

}

それかyのscopeを狭めて借用を使うんですかね。

#[derive(Debug)]

struct Color{

r: u32,

g: u32,

b: u32,

}

fn main() {

let x = Color{r :255, g: 255, b: 255};

{

let y = &x;

println!("{:?}",y);

}

println!("{:?}",x);

}

*1 特性

_ [モフ太] 散歩

夕方、涼しいとまでは言わないが暑くはない

モフ太さんぽ
モフ太さんぽ posted by (C)ひかぞぉ

_ [買物][Office] Microsoft Office 365 Solo (1年版)|オンラインコード版|Win/Mac対応

昨年買ったライセンスを更新しました。10%オフです。

_ [食事]近所の温泉

近所の温泉に行ってみました。いつもより小じんまりしておりますが悪くない。

晩御飯
晩御飯 posted by (C)ひかぞぉ



2002|09|10|11|12|
2003|01|02|03|04|05|06|07|08|09|10|11|12|
2004|01|02|03|04|05|06|07|08|09|10|11|12|
2005|01|02|03|04|05|06|07|08|09|10|11|12|
2006|01|02|03|04|05|06|07|08|09|10|11|12|
2007|01|02|03|04|05|06|07|08|09|10|11|12|
2008|01|02|03|04|05|06|07|08|09|10|11|12|
2009|01|02|03|04|05|06|07|08|09|10|11|12|
2010|01|02|03|04|05|06|07|08|09|10|11|12|
2011|01|02|03|04|05|06|07|08|09|10|11|12|
2012|01|02|03|04|05|06|07|08|09|10|11|12|
2013|01|02|03|04|05|06|07|08|09|10|11|12|
2014|01|02|03|04|05|06|07|08|09|10|11|12|
2015|01|02|03|04|05|06|07|08|09|10|11|12|
2016|01|02|03|04|05|06|07|08|09|10|11|12|
2017|01|02|03|04|05|06|07|08|09|10|11|12|
2018|01|02|03|04|05|06|07|08|09|10|11|12|
2019|01|02|03|04|05|06|07|08|09|10|11|12|
2020|01|02|03|
«前の日記(2017-07-22) 最新 次の日記(2017-07-24)» 編集