1648605600
In this video I will show you how to create a static website with Figma & Astro #6: how to use Astro Structure & Components
ð±âð» Access the course files on GitHub:
https://github.com/coding-in-public/desgn-landing-page
1655630160
Install via pip:
$ pip install pytumblr
Install from source:
$ git clone https://github.com/tumblr/pytumblr.git
$ cd pytumblr
$ python setup.py install
A pytumblr.TumblrRestClient
is the object you'll make all of your calls to the Tumblr API through. Creating one is this easy:
client = pytumblr.TumblrRestClient(
'<consumer_key>',
'<consumer_secret>',
'<oauth_token>',
'<oauth_secret>',
)
client.info() # Grabs the current user information
Two easy ways to get your credentials to are:
interactive_console.py
tool (if you already have a consumer key & secret)client.info() # get information about the authenticating user
client.dashboard() # get the dashboard for the authenticating user
client.likes() # get the likes for the authenticating user
client.following() # get the blogs followed by the authenticating user
client.follow('codingjester.tumblr.com') # follow a blog
client.unfollow('codingjester.tumblr.com') # unfollow a blog
client.like(id, reblogkey) # like a post
client.unlike(id, reblogkey) # unlike a post
client.blog_info(blogName) # get information about a blog
client.posts(blogName, **params) # get posts for a blog
client.avatar(blogName) # get the avatar for a blog
client.blog_likes(blogName) # get the likes on a blog
client.followers(blogName) # get the followers of a blog
client.blog_following(blogName) # get the publicly exposed blogs that [blogName] follows
client.queue(blogName) # get the queue for a given blog
client.submission(blogName) # get the submissions for a given blog
Creating posts
PyTumblr lets you create all of the various types that Tumblr supports. When using these types there are a few defaults that are able to be used with any post type.
The default supported types are described below.
We'll show examples throughout of these default examples while showcasing all the specific post types.
Creating a photo post
Creating a photo post supports a bunch of different options plus the described default options * caption - a string, the user supplied caption * link - a string, the "click-through" url for the photo * source - a string, the url for the photo you want to use (use this or the data parameter) * data - a list or string, a list of filepaths or a single file path for multipart file upload
#Creates a photo post using a source URL
client.create_photo(blogName, state="published", tags=["testing", "ok"],
source="https://68.media.tumblr.com/b965fbb2e501610a29d80ffb6fb3e1ad/tumblr_n55vdeTse11rn1906o1_500.jpg")
#Creates a photo post using a local filepath
client.create_photo(blogName, state="queue", tags=["testing", "ok"],
tweet="Woah this is an incredible sweet post [URL]",
data="/Users/johnb/path/to/my/image.jpg")
#Creates a photoset post using several local filepaths
client.create_photo(blogName, state="draft", tags=["jb is cool"], format="markdown",
data=["/Users/johnb/path/to/my/image.jpg", "/Users/johnb/Pictures/kittens.jpg"],
caption="## Mega sweet kittens")
Creating a text post
Creating a text post supports the same options as default and just a two other parameters * title - a string, the optional title for the post. Supports markdown or html * body - a string, the body of the of the post. Supports markdown or html
#Creating a text post
client.create_text(blogName, state="published", slug="testing-text-posts", title="Testing", body="testing1 2 3 4")
Creating a quote post
Creating a quote post supports the same options as default and two other parameter * quote - a string, the full text of the qote. Supports markdown or html * source - a string, the cited source. HTML supported
#Creating a quote post
client.create_quote(blogName, state="queue", quote="I am the Walrus", source="Ringo")
Creating a link post
#Create a link post
client.create_link(blogName, title="I like to search things, you should too.", url="https://duckduckgo.com",
description="Search is pretty cool when a duck does it.")
Creating a chat post
Creating a chat post supports the same options as default and two other parameters * title - a string, the title of the chat post * conversation - a string, the text of the conversation/chat, with diablog labels (no html)
#Create a chat post
chat = """John: Testing can be fun!
Renee: Testing is tedious and so are you.
John: Aw.
"""
client.create_chat(blogName, title="Renee just doesn't understand.", conversation=chat, tags=["renee", "testing"])
Creating an audio post
Creating an audio post allows for all default options and a has 3 other parameters. The only thing to keep in mind while dealing with audio posts is to make sure that you use the external_url parameter or data. You cannot use both at the same time. * caption - a string, the caption for your post * external_url - a string, the url of the site that hosts the audio file * data - a string, the filepath of the audio file you want to upload to Tumblr
#Creating an audio file
client.create_audio(blogName, caption="Rock out.", data="/Users/johnb/Music/my/new/sweet/album.mp3")
#lets use soundcloud!
client.create_audio(blogName, caption="Mega rock out.", external_url="https://soundcloud.com/skrillex/sets/recess")
Creating a video post
Creating a video post allows for all default options and has three other options. Like the other post types, it has some restrictions. You cannot use the embed and data parameters at the same time. * caption - a string, the caption for your post * embed - a string, the HTML embed code for the video * data - a string, the path of the file you want to upload
#Creating an upload from YouTube
client.create_video(blogName, caption="Jon Snow. Mega ridiculous sword.",
embed="http://www.youtube.com/watch?v=40pUYLacrj4")
#Creating a video post from local file
client.create_video(blogName, caption="testing", data="/Users/johnb/testing/ok/blah.mov")
Editing a post
Updating a post requires you knowing what type a post you're updating. You'll be able to supply to the post any of the options given above for updates.
client.edit_post(blogName, id=post_id, type="text", title="Updated")
client.edit_post(blogName, id=post_id, type="photo", data="/Users/johnb/mega/awesome.jpg")
Reblogging a Post
Reblogging a post just requires knowing the post id and the reblog key, which is supplied in the JSON of any post object.
client.reblog(blogName, id=125356, reblog_key="reblog_key")
Deleting a post
Deleting just requires that you own the post and have the post id
client.delete_post(blogName, 123456) # Deletes your post :(
A note on tags: When passing tags, as params, please pass them as a list (not a comma-separated string):
client.create_text(blogName, tags=['hello', 'world'], ...)
Getting notes for a post
In order to get the notes for a post, you need to have the post id and the blog that it is on.
data = client.notes(blogName, id='123456')
The results include a timestamp you can use to make future calls.
data = client.notes(blogName, id='123456', before_timestamp=data["_links"]["next"]["query_params"]["before_timestamp"])
# get posts with a given tag
client.tagged(tag, **params)
This client comes with a nice interactive console to run you through the OAuth process, grab your tokens (and store them for future use).
You'll need pyyaml
installed to run it, but then it's just:
$ python interactive-console.py
and away you go! Tokens are stored in ~/.tumblr
and are also shared by other Tumblr API clients like the Ruby client.
The tests (and coverage reports) are run with nose, like this:
python setup.py test
Author: tumblr
Source Code: https://github.com/tumblr/pytumblr
License: Apache-2.0 license
1648605600
In this video I will show you how to create a static website with Figma & Astro #6: how to use Astro Structure & Components
ð±âð» Access the course files on GitHub:
https://github.com/coding-in-public/desgn-landing-page
1669003576
In this Python article, let's learn about Mutable and Immutable in Python.
Mutable is a fancy way of saying that the internal state of the object is changed/mutated. So, the simplest definition is: An object whose internal state can be changed is mutable. On the other hand, immutable doesnât allow any change in the object once it has been created.
Both of these states are integral to Python data structure. If you want to become more knowledgeable in the entire Python Data Structure, take this free course which covers multiple data structures in Python including tuple data structure which is immutable. You will also receive a certificate on completion which is sure to add value to your portfolio.
Mutable is when something is changeable or has the ability to change. In Python, âmutableâ is the ability of objects to change their values. These are often the objects that store a collection of data.
Immutable is the when no change is possible over time. In Python, if the value of an object cannot be changed over time, then it is known as immutable. Once created, the value of these objects is permanent.
Objects of built-in type that are mutable are:
Objects of built-in type that are immutable are:
Object mutability is one of the characteristics that makes Python a dynamically typed language. Though Mutable and Immutable in Python is a very basic concept, it can at times be a little confusing due to the intransitive nature of immutability.
In Python, everything is treated as an object. Every object has these three attributes:
While ID and Type cannot be changed once itâs created, values can be changed for Mutable objects.
Check out this free python certificate course to get started with Python.
I believe, rather than diving deep into the theory aspects of mutable and immutable in Python, a simple code would be the best way to depict what it means in Python. Hence, let us discuss the below code step-by-step:
#Creating a list which contains name of Indian cities
cities = [âDelhiâ, âMumbaiâ, âKolkataâ]
# Printing the elements from the list cities, separated by a comma & space
for city in cities:
print(city, end=â, â)
Output [1]: Delhi, Mumbai, Kolkata
#Printing the location of the object created in the memory address in hexadecimal format
print(hex(id(cities)))
Output [2]: 0x1691d7de8c8
#Adding a new city to the list cities
cities.append(âChennaiâ)
#Printing the elements from the list cities, separated by a comma & space
for city in cities:
print(city, end=â, â)
Output [3]: Delhi, Mumbai, Kolkata, Chennai
#Printing the location of the object created in the memory address in hexadecimal format
print(hex(id(cities)))
Output [4]: 0x1691d7de8c8
The above example shows us that we were able to change the internal state of the object âcitiesâ by adding one more city âChennaiâ to it, yet, the memory address of the object did not change. This confirms that we did not create a new object, rather, the same object was changed or mutated. Hence, we can say that the object which is a type of list with reference variable name âcitiesâ is a MUTABLE OBJECT.
Let us now discuss the term IMMUTABLE. Considering that we understood what mutable stands for, it is obvious that the definition of immutable will have âNOTâ included in it. Here is the simplest definition of immutableâ An object whose internal state can NOT be changed is IMMUTABLE.
Again, if you try and concentrate on different error messages, you have encountered, thrown by the respective IDE; you use you would be able to identify the immutable objects in Python. For instance, consider the below code & associated error message with it, while trying to change the value of a Tuple at index 0.
#Creating a Tuple with variable name âfooâ
foo = (1, 2)
#Changing the index[0] value from 1 to 3
foo[0] = 3
TypeError: 'tuple' object does not support item assignment
Once again, a simple code would be the best way to depict what immutable stands for. Hence, let us discuss the below code step-by-step:
#Creating a Tuple which contains English name of weekdays
weekdays = âSundayâ, âMondayâ, âTuesdayâ, âWednesdayâ, âThursdayâ, âFridayâ, âSaturdayâ
# Printing the elements of tuple weekdays
print(weekdays)
Output [1]: (âSundayâ, âMondayâ, âTuesdayâ, âWednesdayâ, âThursdayâ, âFridayâ, âSaturdayâ)
#Printing the location of the object created in the memory address in hexadecimal format
print(hex(id(weekdays)))
Output [2]: 0x1691cc35090
#tuples are immutable, so you cannot add new elements, hence, using merge of tuples with the # + operator to add a new imaginary day in the tuple âweekdaysâ
weekdays += âPythondayâ,
#Printing the elements of tuple weekdays
print(weekdays)
Output [3]: (âSundayâ, âMondayâ, âTuesdayâ, âWednesdayâ, âThursdayâ, âFridayâ, âSaturdayâ, âPythondayâ)
#Printing the location of the object created in the memory address in hexadecimal format
print(hex(id(weekdays)))
Output [4]: 0x1691cc8ad68
This above example shows that we were able to use the same variable name that is referencing an object which is a type of tuple with seven elements in it. However, the ID or the memory location of the old & new tuple is not the same. We were not able to change the internal state of the object âweekdaysâ. The Python program manager created a new object in the memory address and the variable name âweekdaysâ started referencing the new object with eight elements in it. Hence, we can say that the object which is a type of tuple with reference variable name âweekdaysâ is an IMMUTABLE OBJECT.
Also Read: Understanding the Exploratory Data Analysis (EDA) in Python
Where can you use mutable and immutable objects:
Mutable objects can be used where you want to allow for any updates. For example, you have a list of employee names in your organizations, and that needs to be updated every time a new member is hired. You can create a mutable list, and it can be updated easily.
Immutability offers a lot of useful applications to different sensitive tasks we do in a network centred environment where we allow for parallel processing. By creating immutable objects, you seal the values and ensure that no threads can invoke overwrite/update to your data. This is also useful in situations where you would like to write a piece of code that cannot be modified. For example, a debug code that attempts to find the value of an immutable object.
Watch outs: Non transitive nature of Immutability:
OK! Now we do understand what mutable & immutable objects in Python are. Letâs go ahead and discuss the combination of these two and explore the possibilities. Letâs discuss, as to how will it behave if you have an immutable object which contains the mutable object(s)? Or vice versa? Let us again use a code to understand this behaviourâ
#creating a tuple (immutable object) which contains 2 lists(mutable) as itâs elements
#The elements (lists) contains the name, age & gender
person = (['Ayaan', 5, 'Male'], ['Aaradhya', 8, 'Female'])
#printing the tuple
print(person)
Output [1]: (['Ayaan', 5, 'Male'], ['Aaradhya', 8, 'Female'])
#printing the location of the object created in the memory address in hexadecimal format
print(hex(id(person)))
Output [2]: 0x1691ef47f88
#Changing the age for the 1st element. Selecting 1st element of tuple by using indexing [0] then 2nd element of the list by using indexing [1] and assigning a new value for age as 4
person[0][1] = 4
#printing the updated tuple
print(person)
Output [3]: (['Ayaan', 4, 'Male'], ['Aaradhya', 8, 'Female'])
#printing the location of the object created in the memory address in hexadecimal format
print(hex(id(person)))
Output [4]: 0x1691ef47f88
In the above code, you can see that the object âpersonâ is immutable since it is a type of tuple. However, it has two lists as itâs elements, and we can change the state of lists (lists being mutable). So, here we did not change the object reference inside the Tuple, but the referenced object was mutated.
Also Read: Real-Time Object Detection Using TensorFlow
Same way, letâs explore how it will behave if you have a mutable object which contains an immutable object? Let us again use a code to understand the behaviourâ
#creating a list (mutable object) which contains tuples(immutable) as itâs elements
list1 = [(1, 2, 3), (4, 5, 6)]
#printing the list
print(list1)
Output [1]: [(1, 2, 3), (4, 5, 6)]
#printing the location of the object created in the memory address in hexadecimal format
print(hex(id(list1)))
Output [2]: 0x1691d5b13c8
#changing object reference at index 0
list1[0] = (7, 8, 9)
#printing the list
Output [3]: [(7, 8, 9), (4, 5, 6)]
#printing the location of the object created in the memory address in hexadecimal format
print(hex(id(list1)))
Output [4]: 0x1691d5b13c8
As an individual, it completely depends upon you and your requirements as to what kind of data structure you would like to create with a combination of mutable & immutable objects. I hope that this information will help you while deciding the type of object you would like to select going forward.
Before I end our discussion on IMMUTABILITY, allow me to use the word âCAVITEâ when we discuss the String and Integers. There is an exception, and you may see some surprising results while checking the truthiness for immutability. For instance:
#creating an object of integer type with value 10 and reference variable name âxâ
x = 10
#printing the value of âxâ
print(x)
Output [1]: 10
#Printing the location of the object created in the memory address in hexadecimal format
print(hex(id(x)))
Output [2]: 0x538fb560
#creating an object of integer type with value 10 and reference variable name âyâ
y = 10
#printing the value of âyâ
print(y)
Output [3]: 10
#Printing the location of the object created in the memory address in hexadecimal format
print(hex(id(y)))
Output [4]: 0x538fb560
As per our discussion and understanding, so far, the memory address for x & y should have been different, since, 10 is an instance of Integer class which is immutable. However, as shown in the above code, it has the same memory address. This is not something that we expected. It seems that what we have understood and discussed, has an exception as well.
Quick check â Python Data Structures
Tuples are immutable and hence cannot have any changes in them once they are created in Python. This is because they support the same sequence operations as strings. We all know that strings are immutable. The index operator will select an element from a tuple just like in a string. Hence, they are immutable.
Like all, there are exceptions in the immutability in python too. Not all immutable objects are really mutable. This will lead to a lot of doubts in your mind. Let us just take an example to understand this.
Consider a tuple âtupâ.
Now, if we consider tuple tup = (âGreatLearningâ,[4,3,1,2]) ;
We see that the tuple has elements of different data types. The first element here is a string which as we all know is immutable in nature. The second element is a list which we all know is mutable. Now, we all know that the tuple itself is an immutable data type. It cannot change its contents. But, the list inside it can change its contents. So, the value of the Immutable objects cannot be changed but its constituent objects can. change its value.
Mutable Object | Immutable Object |
State of the object can be modified after it is created. | State of the object canât be modified once it is created. |
They are not thread safe. | They are thread safe |
Mutable classes are not final. | It is important to make the class final before creating an immutable object. |
list, dictionary, set, user-defined classes.
int, float, decimal, bool, string, tuple, range.
Lists in Python are mutable data types as the elements of the list can be modified, individual elements can be replaced, and the order of elements can be changed even after the list has been created.
(Examples related to lists have been discussed earlier in this blog.)
Tuple and list data structures are very similar, but one big difference between the data types is that lists are mutable, whereas tuples are immutable. The reason for the tupleâs immutability is that once the elements are added to the tuple and the tuple has been created; it remains unchanged.
A programmer would always prefer building a code that can be reused instead of making the whole data object again. Still, even though tuples are immutable, like lists, they can contain any Python object, including mutable objects.
A set is an iterable unordered collection of data type which can be used to perform mathematical operations (like union, intersection, difference etc.). Every element in a set is unique and immutable, i.e. no duplicate values should be there, and the values canât be changed. However, we can add or remove items from the set as the set itself is mutable.
Strings are not mutable in Python. Strings are a immutable data types which means that its value cannot be updated.
Join Great Learning Academyâs free online courses and upgrade your skills today.
Original article source at: https://www.mygreatlearning.com
1635917640
ãã®ã¢ãžã¥ãŒã«ã§ã¯ãRustã§ããã·ã¥ãããè€åããŒã¿åãæäœããæ¹æ³ã«ã€ããŠèª¬æããŸããããã·ã¥ãããã®ãããªã³ã¬ã¯ã·ã§ã³å ã®ããŒã¿ãå埩åŠçããã«ãŒãåŒãå®è£ ããæ¹æ³ãåŠã³ãŸããæŒç¿ãšããŠãèŠæ±ãããæ³šæãã«ãŒãããæ¡ä»¶ããã¹ãããããŸããŸãªã¿ã€ãã®ããŒã¿ãåŠçããããšã«ãã£ãŠè»ãäœæããRustããã°ã©ã ãäœæããŸãã
ééã³å Žã¯éã³ã³ãã€ã©ã«ãã©ãŠã¶ã€ã³ã¿ãã§ãŒã¹ã§ããèšèªãããŒã«ã«ã«ã€ã³ã¹ããŒã«ããåããŸãã¯ã³ã³ãã€ã©ãå©çšã§ããªãå Žåã¯ãPlaygroundã䜿çšããŠRustã³ãŒãã®èšè¿°ã詊ãããšãã§ããŸãããã®ã³ãŒã¹å šäœãéããŠããµã³ãã«ã³ãŒããšæŒç¿ãžã®Playgroundãªã³ã¯ãæäŸããŸããçŸæç¹ã§RustããŒã«ãã§ãŒã³ã䜿çšã§ããªãå Žåã§ããã³ãŒããæäœã§ããŸãã
Rust Playgroundã§å®è¡ããããã¹ãŠã®ã³ãŒãã¯ãããŒã«ã«ã®éçºç°å¢ã§ã³ã³ãã€ã«ããŠå®è¡ããããšãã§ããŸããã³ã³ãã¥ãŒã¿ãŒããRustã³ã³ãã€ã©ãŒãšå¯Ÿè©±ããããšãèºèºããªãã§ãã ãããRust Playgroundã®è©³çްã«ã€ããŠã¯ãWhat isRustïŒãã芧ãã ãããã¢ãžã¥ãŒã«ã
ãã®ã¢ãžã¥ãŒã«ã§ã¯ã次ã®ããšãè¡ããŸãã
Rustã®ãã1ã€ã®äžè¬çãªã³ã¬ã¯ã·ã§ã³ã®çš®é¡ã¯ãããã·ã¥ãããã§ãããã®HashMap<K, V>
åã¯ãåããŒK
ããã®å€ã«ãããã³ã°ããããšã«ãã£ãŠããŒã¿ãæ ŒçŽããŸãV
ããã¯ãã«å
ã®ããŒã¿ã¯æŽæ°ã€ã³ããã¯ã¹ã䜿çšããŠã¢ã¯ã»ã¹ãããŸãããããã·ã¥ãããå
ã®ããŒã¿ã¯ããŒã䜿çšããŠã¢ã¯ã»ã¹ãããŸãã
ããã·ã¥ãããã¿ã€ãã¯ããªããžã§ã¯ããããã·ã¥ããŒãã«ãèŸæžãªã©ã®ããŒã¿é ç®ã®å€ãã®ããã°ã©ãã³ã°èšèªã§äœ¿çšãããŸãã
ãã¯ãã«ã®ããã«ãããã·ã¥ãããã¯æ¡åŒµå¯èœã§ããããŒã¿ã¯ããŒãã«æ ŒçŽãããããã·ã¥ãããã¢ã€ãã ãžã®ã¢ã¯ã»ã¹ã¯å®è¡æã«ãã§ãã¯ãããŸãã
次ã®äŸã§ã¯ãæžè©ã远跡ããããã®ããã·ã¥ããããå®çŸ©ããŠããŸããããã·ã¥ãããããŒã¯æ¬ã®ååã§ãããå€ã¯èªè ã®ã¬ãã¥ãŒã§ãã
use std::collections::HashMap;
let mut reviews: HashMap<String, String> = HashMap::new();
reviews.insert(String::from("Ancient Roman History"), String::from("Very accurate."));
reviews.insert(String::from("Cooking with Rhubarb"), String::from("Sweet recipes."));
reviews.insert(String::from("Programming in Rust"), String::from("Great examples."));
ãã®ã³ãŒããããã«è©³ãã調ã¹ãŠã¿ãŸããããæåã®è¡ã«ãæ°ããã¿ã€ãã®æ§æã衚瀺ãããŸãã
use std::collections::HashMap;
ãã®use
ã³ãã³ãã¯ãRustæšæºã©ã€ãã©ãªã®äžéšHashMap
ããã®å®çŸ©ãcollections
ããã°ã©ã ã®ã¹ã³ãŒãã«åã蟌ã¿ãŸãããã®æ§æã¯ãä»ã®ããã°ã©ãã³ã°èšèªãã€ã³ããŒããšåŒã¶ãã®ãšäŒŒãŠããŸãã
HashMap::new
ã¡ãœããã䜿çšããŠç©ºã®ããã·ã¥ããããäœæããŸããreviews
å¿
èŠã«å¿ããŠããŒãšå€ã远å ãŸãã¯åé€ã§ããããã«ã倿°ãå¯å€ãšããŠå®£èšããŸãããã®äŸã§ã¯ãããã·ã¥ãããã®ããŒãšå€ã®äž¡æ¹ãString
ã¿ã€ãã䜿çšããŠããŸãã
let mut reviews: HashMap<String, String> = HashMap::new();
ãã®insert(<key>, <value>)
ã¡ãœããã䜿çšããŠãããã·ã¥ãããã«èŠçŽ ã远å ããŸããã³ãŒãã§ã¯ãæ§æã¯<hash_map_name>.insert()
次ã®ãšããã§ãã
reviews.insert(String::from("Ancient Roman History"), String::from("Very accurate."));
ããã·ã¥ãããã«ããŒã¿ã远å ããåŸãget(<key>)
ã¡ãœããã䜿çšããŠããŒã®ç¹å®ã®å€ãååŸã§ããŸãã
// Look for a specific review
let book: &str = "Programming in Rust";
println!("\nReview for \'{}\': {:?}", book, reviews.get(book));
åºåã¯æ¬¡ã®ãšããã§ãã
Review for 'Programming in Rust': Some("Great examples.")
ããŒã
åºåã«ã¯ãæžè©ãåãªãããã°ãããäŸãã§ã¯ãªããSomeïŒ "ãã°ãããäŸã"ïŒããšããŠè¡šç€ºãããŠããããšã«æ³šæããŠãã ãããget
ã¡ãœããã¯Option<&Value>
åãè¿ããããRustã¯ã¡ãœããåŒã³åºãã®çµæããSomeïŒïŒã衚èšã§ã©ããããŸãã
ãã®.remove()
ã¡ãœããã䜿çšããŠãããã·ã¥ããããããšã³ããªãåé€ã§ããŸããget
ç¡å¹ãªããã·ã¥ãããããŒã«å¯ŸããŠã¡ãœããã䜿çšãããšãget
ã¡ãœããã¯ããªãããè¿ããŸãã
// Remove book review
let obsolete: &str = "Ancient Roman History";
println!("\n'{}\' removed.", obsolete);
reviews.remove(obsolete);
// Confirm book review removed
println!("\nReview for \'{}\': {:?}", obsolete, reviews.get(obsolete));
åºåã¯æ¬¡ã®ãšããã§ãã
'Ancient Roman History' removed.
Review for 'Ancient Roman History': None
ãã®ã³ãŒãã詊ããŠããã®RustPlaygroundã§ããã·ã¥ããããæäœã§ããŸãã
æŒç¿ïŒããã·ã¥ãããã䜿çšããŠæ³šæã远跡ãã
ãã®æŒç¿ã§ã¯ãããã·ã¥ãããã䜿çšããããã«èªåè»å·¥å Žã®ããã°ã©ã ã倿ŽããŸãã
ããã·ã¥ãããããŒãšå€ã®ãã¢ã䜿çšããŠãè»ã®æ³šæã«é¢ãã詳现ã远跡ããåºåã衚瀺ããŸããç¹°ãè¿ãã«ãªããŸãããããªãã®èª²é¡ã¯ããµã³ãã«ã³ãŒãã宿ãããŠã³ã³ãã€ã«ããŠå®è¡ããããšã§ãã
ãã®æŒç¿ã®ãµã³ãã«ã³ãŒãã§äœæ¥ããã«ã¯ã次ã®2ã€ã®ãªãã·ã§ã³ããããŸãã
ããŒã
ãµã³ãã«ã³ãŒãã§ã
todo!
ãã¯ããæ¢ããŸãããã®ãã¯ãã¯ãå®äºãããæŽæ°ããå¿ èŠãããã³ãŒãã瀺ããŸãã
æåã®ã¹ãããã¯ãæ¢åã®ããã°ã©ã ã³ãŒããååŸããããšã§ãã
car_quality
ãcar_factory
ããã³main
æ©èœããæ¬¡ã®ã³ãŒããã³ããŒããŠããŒã«ã«éçºç°å¢ã§ç·šéãã
ãããã®æºåãããRustPlaygroundã§ã³ãŒããéããŸãã
#[derive(PartialEq, Debug)]
struct Car { color: String, motor: Transmission, roof: bool, age: (Age, u32) }
#[derive(PartialEq, Debug)]
enum Transmission { Manual, SemiAuto, Automatic }
#[derive(PartialEq, Debug)]
enum Age { New, Used }
// Get the car quality by testing the value of the input argument
// - miles (u32)
// Return tuple with car age ("New" or "Used") and mileage
fn car_quality (miles: u32) -> (Age, u32) {
// Check if car has accumulated miles
// Return tuple early for Used car
if miles > 0 {
return (Age::Used, miles);
}
// Return tuple for New car, no need for "return" keyword or semicolon
(Age::New, miles)
}
// Build "Car" using input arguments
fn car_factory(order: i32, miles: u32) -> Car {
let colors = ["Blue", "Green", "Red", "Silver"];
// Prevent panic: Check color index for colors array, reset as needed
// Valid color = 1, 2, 3, or 4
// If color > 4, reduce color to valid index
let mut color = order as usize;
if color > 4 {
// color = 5 --> index 1, 6 --> 2, 7 --> 3, 8 --> 4
color = color - 4;
}
// Add variety to orders for motor type and roof type
let mut motor = Transmission::Manual;
let mut roof = true;
if order % 3 == 0 { // 3, 6, 9
motor = Transmission::Automatic;
} else if order % 2 == 0 { // 2, 4, 8, 10
motor = Transmission::SemiAuto;
roof = false;
} // 1, 5, 7, 11
// Return requested "Car"
Car {
color: String::from(colors[(color-1) as usize]),
motor: motor,
roof: roof,
age: car_quality(miles)
}
}
fn main() {
// Initialize counter variable
let mut order = 1;
// Declare a car as mutable "Car" struct
let mut car: Car;
// Order 6 cars, increment "order" for each request
// Car order #1: Used, Hard top
car = car_factory(order, 1000);
println!("{}: {:?}, Hard top = {}, {:?}, {}, {} miles", order, car.age.0, car.roof, car.motor, car.color, car.age.1);
// Car order #2: Used, Convertible
order = order + 1;
car = car_factory(order, 2000);
println!("{}: {:?}, Hard top = {}, {:?}, {}, {} miles", order, car.age.0, car.roof, car.motor, car.color, car.age.1);
// Car order #3: New, Hard top
order = order + 1;
car = car_factory(order, 0);
println!("{}: {:?}, Hard top = {}, {:?}, {}, {} miles", order, car.age.0, car.roof, car.motor, car.color, car.age.1);
// Car order #4: New, Convertible
order = order + 1;
car = car_factory(order, 0);
println!("{}: {:?}, Hard top = {}, {:?}, {}, {} miles", order, car.age.0, car.roof, car.motor, car.color, car.age.1);
// Car order #5: Used, Hard top
order = order + 1;
car = car_factory(order, 3000);
println!("{}: {:?}, Hard top = {}, {:?}, {}, {} miles", order, car.age.0, car.roof, car.motor, car.color, car.age.1);
// Car order #6: Used, Hard top
order = order + 1;
car = car_factory(order, 4000);
println!("{}: {:?}, Hard top = {}, {:?}, {}, {} miles", order, car.age.0, car.roof, car.motor, car.color, car.age.1);
}
2. ããã°ã©ã ããã«ãããŸããæ¬¡ã®ã»ã¯ã·ã§ã³ã«é²ãåã«ãã³ãŒããã³ã³ãã€ã«ãããŠå®è¡ãããããšã確èªããŠãã ããã
次ã®åºåã衚瀺ãããŸãã
1: Used, Hard top = true, Manual, Blue, 1000 miles
2: Used, Hard top = false, SemiAuto, Green, 2000 miles
3: New, Hard top = true, Automatic, Red, 0 miles
4: New, Hard top = false, SemiAuto, Silver, 0 miles
5: Used, Hard top = true, Manual, Blue, 3000 miles
6: Used, Hard top = true, Automatic, Green, 4000 miles
çŸåšã®ããã°ã©ã ã¯ãåè»ã®æ³šæãåŠçããåæ³šæãå®äºããåŸã«èŠçŽãå°å·ããŸããcar_factory
颿°ãåŒã³åºããã³ã«Car
ãæ³šæã®è©³çްãå«ãæ§é äœãè¿ãããæ³šæãå®è¡ãããŸããçµæã¯car
倿°ã«æ ŒçŽãããŸãã
ãæ°ã¥ããããããŸãããããã®ããã°ã©ã ã«ã¯ããã€ãã®éèŠãªæ©èœããããŸããããã¹ãŠã®æ³šæã远跡ããŠããããã§ã¯ãããŸãããcar
倿°ã¯ãçŸåšã®æ³šæã®è©³çްã®ã¿ãä¿æããŠããŸãã颿°car
ã®çµæã§å€æ°ãæŽæ°ããããã³car_factory
ã«ãåã®é åºã®è©³çްãäžæžããããŸãã
ãã¡ã€ãªã³ã°ã·ã¹ãã ã®ããã«ãã¹ãŠã®æ³šæã远跡ããããã«ãããã°ã©ã ãæŽæ°ããå¿
èŠããããŸãããã®ç®çã®ããã«ã<KãV>ãã¢ã§ããã·ã¥ããããå®çŸ©ããŸããããã·ã¥ãããããŒã¯ãè»ã®æ³šæçªå·ã«å¯Ÿå¿ããŸããããã·ã¥ãããå€ã¯ãCar
æ§é äœã§å®çŸ©ãããŠããããããã®æ³šæã®è©³çްã«ãªããŸãã
main
颿°ã®å
é ãæåã®äžæ¬åŒ§ã®çŽåŸã«æ¬¡ã®ã³ãŒãã远å ããŸã{
ã// Initialize a hash map for the car orders
// - Key: Car order number, i32
// - Value: Car order details, Car struct
use std::collections::HashMap;
let mut orders: HashMap<i32, Car> = HashMap;
2. orders
ããã·ã¥ããããäœæããã¹ããŒãã¡ã³ãã®æ§æã®åé¡ãä¿®æ£ããŸãã
ãã³ã
ããã·ã¥ããããæåããäœæããŠããã®ã§ããããããã®
new()
ã¡ãœããã䜿çšããããšããå§ãããŸãã
3. ããã°ã©ã ããã«ãããŸããæ¬¡ã®ã»ã¯ã·ã§ã³ã«é²ãåã«ãã³ãŒããã³ã³ãã€ã«ãããŠããããšã確èªããŠãã ãããã³ã³ãã€ã©ããã®èŠåã¡ãã»ãŒãžã¯ç¡èŠããŠããŸããŸããã
次ã®ã¹ãããã¯ãå±¥è¡ãããåèªåè»æ³šæãããã·ã¥ãããã«è¿œå ããããšã§ãã
ãã®main
颿°ã§ã¯ãcar_factory
è»ã®æ³šæããšã«é¢æ°ãåŒã³åºããŸããæ³šæãå±¥è¡ãããåŸãprintln!
ãã¯ããåŒã³åºããŠãcar
倿°ã«æ ŒçŽãããŠããæ³šæã®è©³çްã衚瀺ããŸãã
// Car order #1: Used, Hard top
car = car_factory(order, 1000);
println!("{}: {}, Hard top = {}, {:?}, {}, {} miles", order, car.age.0, car.roof, car.motor, car.color, car.age.1);
...
// Car order #6: Used, Hard top
order = order + 1;
car = car_factory(order, 4000);
println!("{}: {}, Hard top = {}, {:?}, {}, {} miles", order, car.age.0, car.roof, car.motor, car.color, car.age.1);
æ°ããããã·ã¥ãããã§æ©èœããããã«ããããã®ã³ãŒãã¹ããŒãã¡ã³ããä¿®æ£ããŸãã
car_factory
颿°ã®åŒã³åºãã¯ä¿æããŸããè¿ãããåCar
æ§é äœã¯ãããã·ã¥ãããã®<KãV>ãã¢ã®äžéšãšããŠæ ŒçŽãããŸããprintln!
ãã¯ãã®åŒã³åºããæŽæ°ããŠãããã·ã¥ãããã«ä¿åãããŠããæ³šæã®è©³çްã衚瀺ããŸããmain
颿°ã§ã颿°ã®åŒã³åºãcar_factory
ãšããã«äŒŽãprintln!
ãã¯ãã®åŒã³åºããèŠã€ããŸãã// Car order #1: Used, Hard top
car = car_factory(order, 1000);
println!("{}: {}, Hard top = {}, {:?}, {}, {} miles", order, car.age.0, car.roof, car.motor, car.color, car.age.1);
...
// Car order #6: Used, Hard top
order = order + 1;
car = car_factory(order, 4000);
println!("{}: {}, Hard top = {}, {:?}, {}, {} miles", order, car.age.0, car.roof, car.motor, car.color, car.age.1);
2. ãã¹ãŠã®èªåè»æ³šæã®ã¹ããŒãã¡ã³ãã®å®å šãªã»ãããæ¬¡ã®æ¹èšãããã³ãŒãã«çœ®ãæããŸãã
// Car order #1: Used, Hard top
car = car_factory(order, 1000);
orders(order, car);
println!("Car order {}: {:?}", order, orders.get(&order));
// Car order #2: Used, Convertible
order = order + 1;
car = car_factory(order, 2000);
orders(order, car);
println!("Car order {}: {:?}", order, orders.get(&order));
// Car order #3: New, Hard top
order = order + 1;
car = car_factory(order, 0);
orders(order, car);
println!("Car order {}: {:?}", order, orders.get(&order));
// Car order #4: New, Convertible
order = order + 1;
car = car_factory(order, 0);
orders(order, car);
println!("Car order {}: {:?}", order, orders.get(&order));
// Car order #5: Used, Hard top
order = order + 1;
car = car_factory(order, 3000);
orders(order, car);
println!("Car order {}: {:?}", order, orders.get(&order));
// Car order #6: Used, Hard top
order = order + 1;
car = car_factory(order, 4000);
orders(order, car);
println!("Car order {}: {:?}", order, orders.get(&order));
3. ä»ããããã°ã©ã ããã«ãããããšãããšãã³ã³ãã€ã«ãšã©ãŒã衚瀺ãããŸãã<KãV>ãã¢ãorders
ããã·ã¥ãããã«è¿œå ããã¹ããŒãã¡ã³ãã«æ§æäžã®åé¡ããããŸããåé¡ããããŸããïŒå
ã«é²ãã§ãããã·ã¥ãããã«é åºã远å ããåã¹ããŒãã¡ã³ãã®åé¡ãä¿®æ£ããŠãã ããã
ãã³ã
orders
ããã·ã¥ãããã«çŽæ¥å€ãå²ãåœãŠãããšã¯ã§ããŸãããæ¿å ¥ãè¡ãã«ã¯ã¡ãœããã䜿çšããå¿ èŠããããŸãã
ããã°ã©ã ãæ£åžžã«ãã«ãããããšã次ã®åºåã衚瀺ãããŸãã
Car order 1: Some(Car { color: "Blue", motor: Manual, roof: true, age: ("Used", 1000) })
Car order 2: Some(Car { color: "Green", motor: SemiAuto, roof: false, age: ("Used", 2000) })
Car order 3: Some(Car { color: "Red", motor: Automatic, roof: true, age: ("New", 0) })
Car order 4: Some(Car { color: "Silver", motor: SemiAuto, roof: false, age: ("New", 0) })
Car order 5: Some(Car { color: "Blue", motor: Manual, roof: true, age: ("Used", 3000) })
Car order 6: Some(Car { color: "Green", motor: Automatic, roof: true, age: ("Used", 4000) })
æ¹èšãããã³ãŒãã®åºåãç°ãªãããšã«æ³šæããŠãã ãããprintln!
ãã¯ããã£ã¹ãã¬ã€ã®å
容Car
åå€ã瀺ãããšã«ãã£ãŠãæ§é äœãšå¯Ÿå¿ãããã£ãŒã«ãåã
æ¬¡ã®æŒç¿ã§ã¯ãã«ãŒãåŒã䜿çšããŠã³ãŒãã®åé·æ§ãæžãããŸãã
forãwhileãããã³loopåŒã䜿çšããŸã
å€ãã®å Žåãããã°ã©ã ã«ã¯ããã®å Žã§ç¹°ãè¿ãå¿
èŠã®ããã³ãŒãã®ãããã¯ããããŸããã«ãŒãåŒã䜿çšããŠãç¹°ãè¿ãã®å®è¡æ¹æ³ãããã°ã©ã ã«æç€ºã§ããŸããé»è©±åž³ã®ãã¹ãŠã®ãšã³ããªãå°å·ããã«ã¯ãã«ãŒãåŒã䜿çšããŠãæåã®ãšã³ããªããæåŸã®ãšã³ããªãŸã§å°å·ããæ¹æ³ãããã°ã©ã ã«æç€ºã§ããŸãã
Rustã¯ãããã°ã©ã ã«ã³ãŒãã®ãããã¯ãç¹°ãè¿ãããããã®3ã€ã®ã«ãŒãåŒãæäŸããŸãã
loop
ïŒæå忢ãçºçããªãéããç¹°ãè¿ããŸããwhile
ïŒæ¡ä»¶ãçã®ãŸãŸã§ç¹°ãè¿ããŸããfor
ïŒã³ã¬ã¯ã·ã§ã³å
ã®ãã¹ãŠã®å€ã«å¯ŸããŠç¹°ãè¿ããŸãããã®åå ã§ã¯ããããã®åã«ãŒãåŒãèŠãŠãããŸãã
loop
åŒã¯ãç¡éã«ãŒããäœæããŸãããã®ããŒã¯ãŒãã䜿çšãããšãåŒã®æ¬æã§ã¢ã¯ã·ã§ã³ãç¶ç¶çã«ç¹°ãè¿ãããšãã§ããŸããã«ãŒãã忢ãããããã®çŽæ¥ã¢ã¯ã·ã§ã³ãå®è¡ãããŸã§ãã¢ã¯ã·ã§ã³ãç¹°ãè¿ãããŸãã
次ã®äŸã§ã¯ããWe loopforeverïŒããšããããã¹ããåºåããŸãããããŠããã¯ããèªäœã§æ¢ãŸããŸãããprintln!
ã¢ã¯ã·ã§ã³ã¯ç¹°ãè¿ãç¶ããŸãã
loop {
println!("We loop forever!");
}
loop
åŒã䜿çšããå Žåãã«ãŒãã忢ããå¯äžã®æ¹æ³ã¯ãããã°ã©ããŒãšããŠçŽæ¥ä»å
¥ããå Žåã§ããç¹å®ã®ã³ãŒãã远å ããŠã«ãŒãã忢ããããCtrl + Cãªã©ã®ããŒããŒãåœä»€ãå
¥åããŠããã°ã©ã ã®å®è¡ã忢ãããã§ããŸãã
loop
åŒã忢ããæãäžè¬çãªæ¹æ³ã¯ãbreak
ããŒã¯ãŒãã䜿çšããŠãã¬ãŒã¯ãã€ã³ããèšå®ããããšã§ãã
loop {
// Keep printing, printing, printing...
println!("We loop forever!");
// On the other hand, maybe we should stop!
break;
}
ããã°ã©ã ãbreak
ããŒã¯ãŒããæ€åºãããšãloop
åŒã®æ¬äœã§ã¢ã¯ã·ã§ã³ã®å®è¡ã忢ããæ¬¡ã®ã³ãŒãã¹ããŒãã¡ã³ãã«é²ã¿ãŸãã
break
ããŒã¯ãŒãã¯ãç¹å¥ãªæ©èœãæããã«ããloop
衚çŸããbreak
ããŒã¯ãŒãã䜿çšãããšãåŒæ¬äœã§ã®ã¢ã¯ã·ã§ã³ã®ç¹°ãè¿ãã忢ããããšãããã¬ãŒã¯ãã€ã³ãã§å€ãè¿ãããšãã§ããŸãã
次ã®äŸã¯break
ãloop
åŒã§ããŒã¯ãŒãã䜿çšããŠå€ãè¿ãæ¹æ³ã瀺ããŠããŸãã
let mut counter = 1;
// stop_loop is set when loop stops
let stop_loop = loop {
counter *= 2;
if counter > 100 {
// Stop loop, return counter value
break counter;
}
};
// Loop should break when counter = 128
println!("Break the loop at counter = {}.", stop_loop);
åºåã¯æ¬¡ã®ãšããã§ãã
Break the loop at counter = 128.
ç§ãã¡ã®loop
衚çŸã®æ¬äœã¯ããããã®é£ç¶ããã¢ã¯ã·ã§ã³ãå®è¡ããŸãã
stop_loop
倿°ã宣èšããŸããloop
åŒã®çµæã«ãã€ã³ãããããã«ããã°ã©ã ã«æç€ºããŸããloop
åŒã®æ¬äœã§ã¢ã¯ã·ã§ã³ãå®è¡ããŸãïŒcounter
å€ãçŸåšã®å€ã®2åã«ã€ã³ã¯ãªã¡ã³ãããŸããcounter
å€ã確èªããŠãã ãããcounter
å€ã100以äžã§ããã«ãŒãããæãåºãã
counter
å€ãè¿ããŸãã
4. ããcounter
å€ã100以äžã§ã¯ãããŸããã
ã«ãŒãæ¬äœã§ã¢ã¯ã·ã§ã³ãç¹°ãè¿ããŸãã
5. stop_loop
å€ãåŒã®counter
çµæã§ããå€ã«èšå®ããŸãloop
ã
loop
åŒæ¬äœã¯ãè€æ°ã®ãã¬ãŒã¯ãã€ã³ããæã€ããšãã§ããŸããåŒã«è€æ°ã®ãã¬ãŒã¯ãã€ã³ããããå Žåããã¹ãŠã®ãã¬ãŒã¯ãã€ã³ãã¯åãã¿ã€ãã®å€ãè¿ãå¿
èŠããããŸãããã¹ãŠã®å€ã¯ãæŽæ°åãæåååãããŒã«åãªã©ã§ããå¿
èŠããããŸãããã¬ãŒã¯ãã€ã³ããæç€ºçã«å€ãè¿ããªãå Žåãããã°ã©ã ã¯åŒã®çµæã空ã®ã¿ãã«ãšããŠè§£éããŸã()
ã
while
ã«ãŒãã¯ãæ¡ä»¶åŒã䜿çšããŠããŸããæ¡ä»¶åŒãçã§ããéããã«ãŒããç¹°ãè¿ãããŸãããã®ããŒã¯ãŒãã䜿çšãããšãæ¡ä»¶åŒãfalseã«ãªããŸã§ãåŒæ¬äœã®ã¢ã¯ã·ã§ã³ãå®è¡ã§ããŸãã
while
ã«ãŒãã¯ãããŒã«æ¡ä»¶åŒãè©äŸ¡ããããšããå§ãŸããŸããæ¡ä»¶åŒããšè©äŸ¡ãããtrue
ãšãæ¬äœã®ã¢ã¯ã·ã§ã³ãå®è¡ãããŸããã¢ã¯ã·ã§ã³ãå®äºãããšãå¶åŸ¡ã¯æ¡ä»¶åŒã«æ»ããŸããæ¡ä»¶åŒããšè©äŸ¡ãããfalse
ãšãwhile
åŒã¯åæ¢ããŸãã
次ã®äŸã§ã¯ãããã°ããã«ãŒãããŸã...ããšããããã¹ããåºåããŸããã«ãŒããç¹°ãè¿ããã³ã«ããã«ãŠã³ãã5æªæºã§ããããšããæ¡ä»¶ããã¹ããããŸããæ¡ä»¶ãçã®ãŸãŸã§ããéãåŒæ¬äœã®ã¢ã¯ã·ã§ã³ãå®è¡ãããŸããæ¡ä»¶ãçã§ãªããªã£ãåŸãwhile
ã«ãŒãã¯åæ¢ããããã°ã©ã ã¯æ¬¡ã®ã³ãŒãã¹ããŒãã¡ã³ãã«é²ã¿ãŸãã
while counter < 5 {
println!("We loop a while...");
counter = counter + 1;
}
for
ã«ãŒãã¯ãé
ç®ã®ã³ã¬ã¯ã·ã§ã³ãåŠçããããã«ã€ãã¬ãŒã¿ã䜿çšããŠããŸããã«ãŒãã¯ãã³ã¬ã¯ã·ã§ã³å
ã®åã¢ã€ãã ã®åŒæ¬äœã®ã¢ã¯ã·ã§ã³ãç¹°ãè¿ããŸãããã®ã¿ã€ãã®ã«ãŒãã®ç¹°ãè¿ãã¯ãå埩ãšåŒã°ããŸãããã¹ãŠã®å埩ãå®äºãããšãã«ãŒãã¯åæ¢ããŸãã
Rustã§ã¯ãé åããã¯ãã«ãããã·ã¥ããããªã©ãä»»æã®ã³ã¬ã¯ã·ã§ã³ã¿ã€ããå埩åŠçã§ããŸããRustã¯ã€ãã¬ãŒã¿ã䜿çšããŠãã³ã¬ã¯ã·ã§ã³å ã®åã¢ã€ãã ãæåããæåŸãŸã§ç§»åããŸãã
for
ã«ãŒãã¯ã€ãã¬ãŒã¿ãšããŠäžæå€æ°ã䜿çšããŠããŸãã倿°ã¯ã«ãŒãåŒã®éå§æã«æé»çã«å®£èšãããçŸåšã®å€ã¯å埩ããšã«èšå®ãããŸãã
次ã®ã³ãŒãã§ã¯ãã³ã¬ã¯ã·ã§ã³ã¯big_birds
é
åã§ãããã€ãã¬ãŒã¿ãŒã®ååã¯bird
ã§ãã
let big_birds = ["ostrich", "peacock", "stork"];
for bird in big_birds
iter()
ã¡ãœããã䜿çšããŠãã³ã¬ã¯ã·ã§ã³å
ã®ã¢ã€ãã ã«ã¢ã¯ã»ã¹ããŸããfor
åŒã¯çµæã«ã€ãã¬ãŒã¿ã®çŸåšã®å€ããã€ã³ãããiter()
æ¹æ³ãåŒæ¬äœã§ã¯ãã€ãã¬ãŒã¿å€ãæäœã§ããŸãã
let big_birds = ["ostrich", "peacock", "stork"];
for bird in big_birds.iter() {
println!("The {} is a big bird.", bird);
}
åºåã¯æ¬¡ã®ãšããã§ãã
The ostrich is a big bird.
The peacock is a big bird.
The stork is a big bird.
ã€ãã¬ãŒã¿ãäœæãããã1ã€ã®ç°¡åãªæ¹æ³ã¯ãç¯å²è¡šèšã䜿çšããããšã§ãa..b
ãã€ãã¬ãŒã¿ã¯a
å€ããå§ãŸãb
ã1ã¹ããããã€ç¶ããŸãããå€ã䜿çšããŸããb
ã
for number in 0..5 {
println!("{}", number * 2);
}
ãã®ã³ãŒãã¯ã0ã1ã2ã3ãããã³4ã®æ°å€ãnumber
ç¹°ãè¿ãåŠçããŸããã«ãŒãã®ç¹°ãè¿ãããšã«ãå€ã倿°ã«ãã€ã³ãããŸãã
åºåã¯æ¬¡ã®ãšããã§ãã
0
2
4
6
8
ãã®ã³ãŒããå®è¡ããŠããã®RustPlaygroundã§ã«ãŒããæ¢çŽ¢ã§ããŸãã
æŒç¿ïŒã«ãŒãã䜿çšããŠããŒã¿ãå埩åŠçãã
ãã®æŒç¿ã§ã¯ãèªåè»å·¥å Žã®ããã°ã©ã ã倿ŽããŠãã«ãŒãã䜿çšããŠèªåè»ã®æ³šæãå埩åŠçããŸãã
main
颿°ãæŽæ°ããŠã泚æã®å®å
šãªã»ãããåŠçããããã®ã«ãŒãåŒã远å ããŸããã«ãŒãæ§é ã¯ãã³ãŒãã®åé·æ§ãæžããã®ã«åœ¹ç«ã¡ãŸããã³ãŒããç°¡çŽ åããããšã§ã泚æéãç°¡åã«å¢ããããšãã§ããŸãã
ãã®car_factory
颿°ã§ã¯ãç¯å²å€ã®å€ã§ã®å®è¡æã®ãããã¯ãåé¿ããããã«ãå¥ã®ã«ãŒãã远å ããŸãã
課é¡ã¯ããµã³ãã«ã³ãŒãã宿ãããŠãã³ã³ãã€ã«ããŠå®è¡ããããšã§ãã
ãã®æŒç¿ã®ãµã³ãã«ã³ãŒãã§äœæ¥ããã«ã¯ã次ã®2ã€ã®ãªãã·ã§ã³ããããŸãã
ããŒã
ãµã³ãã«ã³ãŒãã§ã
todo!
ãã¯ããæ¢ããŸãããã®ãã¯ãã¯ãå®äºãããæŽæ°ããå¿ èŠãããã³ãŒãã瀺ããŸãã
ååã®æŒç¿ã§ããã°ã©ã ã³ãŒããéããå Žåã¯ããã®æºåãããRustPlaygroundã§ã³ãŒããå床éãããšãã§ããŸãã
å¿ ãããã°ã©ã ãåæ§ç¯ããã³ã³ãã€ã©ãšã©ãŒãªãã§å®è¡ãããããšã確èªããŠãã ããã
ããå€ãã®æ³šæããµããŒãããã«ã¯ãããã°ã©ã ãæŽæ°ããå¿ èŠããããŸããçŸåšã®ã³ãŒãæ§é ã§ã¯ãåé·ã¹ããŒãã¡ã³ãã䜿çšããŠ6ã€ã®æ³šæããµããŒãããŠããŸããåé·æ§ã¯æ±ãã«ãããç¶æããã®ãå°é£ã§ãã
ã«ãŒãåŒã䜿çšããŠã¢ã¯ã·ã§ã³ãç¹°ãè¿ããåæ³šæãäœæããããšã§ãæ§é ãåçŽåã§ããŸããç°¡ç¥åãããã³ãŒãã䜿çšãããšã倿°ã®æ³šæããã°ããäœæã§ããŸãã
main
æ©èœãå逿¬¡ã®æãããã®ã³ãŒããããã¯ã¯ãorder
倿°ãå®çŸ©ããã³èšå®ããèªåè»ã®æ³šæã®car_factory
颿°ãšprintln!
ãã¯ããåŒã³åºããåæ³šæãorders
ããã·ã¥ãããã«æ¿å
¥ããŸãã// Order 6 cars
// - Increment "order" after each request
// - Add each order <K, V> pair to "orders" hash map
// - Call println! to show order details from the hash map
// Initialize order variable
let mut order = 1;
// Car order #1: Used, Hard top
car = car_factory(order, 1000);
orders.insert(order, car);
println!("Car order {}: {:?}", order, orders.get(&order));
...
// Car order #6: Used, Hard top
order = order + 1;
car = car_factory(order, 4000);
orders.insert(order, car);
println!("Car order {}: {:?}", order, orders.get(&order));
2. åé€ãããã¹ããŒãã¡ã³ããæ¬¡ã®ã³ãŒããããã¯ã«çœ®ãæããŸãã
// Start with zero miles
let mut miles = 0;
todo!("Add a loop expression to fulfill orders for 6 cars, initialize `order` variable to 1") {
// Call car_factory to fulfill order
// Add order <K, V> pair to "orders" hash map
// Call println! to show order details from the hash map
car = car_factory(order, miles);
orders.insert(order, car);
println!("Car order {}: {:?}", order, orders.get(&order));
// Reset miles for order variety
if miles == 2100 {
miles = 0;
} else {
miles = miles + 700;
}
}
3. ã¢ã¯ã·ã§ã³ãç¹°ãè¿ãã«ãŒãåŒã远å ããŠã6å°ã®è»ã®æ³šæãäœæããŸããorder
1ã«åæåããã倿°ãå¿
èŠã§ãã
4. ããã°ã©ã ããã«ãããŸããã³ãŒãããšã©ãŒãªãã§ã³ã³ãã€ã«ãããããšã確èªããŠãã ããã
次ã®äŸã®ãããªåºåã衚瀺ãããŸãã
Car order 1: Some(Car { color: "Blue", motor: Manual, roof: true, age: ("New", 0) })
Car order 2: Some(Car { color: "Green", motor: SemiAuto, roof: false, age: ("Used", 700) })
Car order 3: Some(Car { color: "Red", motor: Automatic, roof: true, age: ("Used", 1400) })
Car order 4: Some(Car { color: "Silver", motor: SemiAuto, roof: false, age: ("Used", 2100) })
Car order 5: Some(Car { color: "Blue", motor: Manual, roof: true, age: ("New", 0) })
Car order 6: Some(Car { color: "Green", motor: Automatic, roof: true, age: ("Used", 700) })
ããã°ã©ã ã¯çŸåšãã«ãŒãã䜿çšããŠ6å°ã®è»ã®æ³šæãåŠçããŠããŸãã6å°ä»¥äžæ³šæãããšã©ããªããŸããïŒ
main
颿°ã®ã«ãŒãåŒãæŽæ°ããŠã11å°ã®è»ã泚æããŸãã todo!("Update the loop expression to create 11 cars");
2. ããã°ã©ã ãåæ§ç¯ããŸããå®è¡æã«ãããã°ã©ã ã¯ãããã¯ã«ãªããŸãïŒ
Compiling playground v0.0.1 (/playground)
Finished dev [unoptimized + debuginfo] target(s) in 1.26s
Running `target/debug/playground`
thread 'main' panicked at 'index out of bounds: the len is 4 but the index is 4', src/main.rs:34:29
ãã®åé¡ã解決ããæ¹æ³ãèŠãŠã¿ãŸãããã
ãã®car_factory
颿°ã§ã¯ãif / elseåŒã䜿çšcolor
ããŠãcolors
é
åã®ã€ã³ããã¯ã¹ã®å€ã確èªããŸãã
// Prevent panic: Check color index for colors array, reset as needed
// Valid color = 1, 2, 3, or 4
// If color > 4, reduce color to valid index
let mut color = order as usize;
if color > 4 {
// color = 5 --> index 1, 6 --> 2, 7 --> 3, 8 --> 4
color = color - 4;
}
colors
é
åã«ã¯4ã€ã®èŠçŽ ãæã¡ããã€æå¹ãªcolor
å Žåã¯ãã€ã³ããã¯ã¹ã®ç¯å²ã¯0ã3ã®æ¡ä»¶åŒããã§ãã¯ããŠããcolor
ç§ãã¡ã¯ããã§ãã¯ããŸããïŒã€ã³ããã¯ã¹ã4ããã倧ããå Žåcolor
ããã®åŸã®é¢æ°ã§4ã«çããã€ã³ããã¯ã¹ãžã®ãšãã«æã
ã®ã€ã³ããã¯ã¹ãè»ã®è²ãå²ãåœãŠãé
åã§ã¯ãã€ã³ããã¯ã¹å€ãã1ãæžç®ããŸãcolor - 1
ãcolor
å€4ã¯colors[3]
ãé
åãšåæ§ã«åŠçãããŸããïŒ
çŸåšã®if / elseåŒã¯ã8å°ä»¥äžã®è»ã泚æãããšãã®å®è¡æã®ãããã¯ãé²ãããã«ããŸãæ©èœããŸãããããã11å°ã®è»ã泚æãããšãããã°ã©ã ã¯9çªç®ã®æ³šæã§ãããã¯ã«ãªããŸããããå ç¢ã«ãªãããã«åŒã調æŽããå¿ èŠããããŸãããã®æ¹åãè¡ãããã«ãå¥ã®ã«ãŒãåŒã䜿çšããŸãã
car_factory
æ©èœãã«ãŒãåŒã§ããã°/ä»ã®æ¡ä»¶æã亀æããŠãã ãããcolor
ã€ã³ããã¯ã¹å€ã4ãã倧ããå Žåã«å®è¡æã®ãããã¯ãé²ãããã«ãæ¬¡ã®æ¬äŒŒã³ãŒãã¹ããŒãã¡ã³ããä¿®æ£ããŠãã ããã// Prevent panic: Check color index, reset as needed
// If color = 1, 2, 3, or 4 - no change needed
// If color > 4, reduce to color to a valid index
let mut color = order as usize;
todo!("Replace `if/else` condition with a loop to prevent run-time panic for color > 4");
ãã³ã
ãã®å Žåãif / elseæ¡ä»¶ããã«ãŒãåŒãžã®å€æŽã¯å®éã«ã¯éåžžã«ç°¡åã§ãã
2. ããã°ã©ã ããã«ãããŸããã³ãŒãããšã©ãŒãªãã§ã³ã³ãã€ã«ãããããšã確èªããŠãã ããã
次ã®åºåã衚瀺ãããŸãã
Car order 1: Some(Car { color: "Blue", motor: Manual, roof: true, age: ("New", 0) })
Car order 2: Some(Car { color: "Green", motor: SemiAuto, roof: false, age: ("Used", 700) })
Car order 3: Some(Car { color: "Red", motor: Automatic, roof: true, age: ("Used", 1400) })
Car order 4: Some(Car { color: "Silver", motor: SemiAuto, roof: false, age: ("Used", 2100) })
Car order 5: Some(Car { color: "Blue", motor: Manual, roof: true, age: ("New", 0) })
Car order 6: Some(Car { color: "Green", motor: Automatic, roof: true, age: ("Used", 700) })
Car order 7: Some(Car { color: "Red", motor: Manual, roof: true, age: ("Used", 1400) })
Car order 8: Some(Car { color: "Silver", motor: SemiAuto, roof: false, age: ("Used", 2100) })
Car order 9: Some(Car { color: "Blue", motor: Automatic, roof: true, age: ("New", 0) })
Car order 10: Some(Car { color: "Green", motor: SemiAuto, roof: false, age: ("Used", 700) })
Car order 11: Some(Car { color: "Red", motor: Manual, roof: true, age: ("Used", 1400) })
ãã®ã¢ãžã¥ãŒã«ã§ã¯ãRustã§äœ¿çšã§ããããŸããŸãªã«ãŒãåŒã調ã¹ãããã·ã¥ãããã®æäœæ¹æ³ãçºèŠããŸãããããŒã¿ã¯ãããŒãšå€ã®ãã¢ãšããŠããã·ã¥ãããã«ä¿åãããŸããããã·ã¥ãããã¯æ¡åŒµå¯èœã§ãã
loop
æåã§ããã»ã¹ã忢ãããŸã§ã®åŒã¯ãã¢ã¯ã·ã§ã³ãç¹°ãè¿ããŸããwhile
åŒãã«ãŒãããŠãæ¡ä»¶ãçã§ããéãã¢ã¯ã·ã§ã³ãç¹°ãè¿ãããšãã§ããŸãããã®for
åŒã¯ãããŒã¿åéãå埩åŠçããããã«äœ¿çšãããŸãã
ãã®æŒç¿ã§ã¯ãèªåè»ããã°ã©ã ãæ¡åŒµããŠãç¹°ãè¿ãããã¢ã¯ã·ã§ã³ãã«ãŒããããã¹ãŠã®æ³šæãåŠçããŸãããæ³šæã远跡ããããã«ããã·ã¥ããããå®è£ ããŸããã
ãã®ã©ãŒãã³ã°ãã¹ã®æ¬¡ã®ã¢ãžã¥ãŒã«ã§ã¯ãRustã³ãŒãã§ãšã©ãŒãšé害ãã©ã®ããã«åŠçããããã«ã€ããŠè©³ãã説æããŸãã
ãªã³ã¯: https://docs.microsoft.com/en-us/learn/modules/rust-loop-expressions/
1619013192
Demo Click Here: https://cutt.ly/2vFKuxe
#portfolio website html css #personal website html css #personal portfolio website #how to create a complete peronal portfolio website #responsive portfolio website html css #responsive personal portfolio website html css